mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-16 19:43:15 +03:00
Merge ea5830a929
into f2da51e0d4
This commit is contained in:
commit
fa757fb5c0
52
docs/examples/fastdepends.rst
Normal file
52
docs/examples/fastdepends.rst
Normal file
|
@ -0,0 +1,52 @@
|
|||
.. _fastdepends-example:
|
||||
|
||||
FastDepends example
|
||||
===================
|
||||
|
||||
.. meta::
|
||||
:keywords: Python,Dependency Injection,FastDepends,Example
|
||||
:description: This example demonstrates a usage of the FastDepends and Dependency Injector.
|
||||
|
||||
|
||||
This example shows how to use ``Dependency Injector`` with `FastDepends <https://github.com/Lancetnik/FastDepends>`_.
|
||||
|
||||
Example code is available on `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/tests/unit/wiringfastdepends>`_.
|
||||
|
||||
Quick sample
|
||||
------------
|
||||
|
||||
Just use it within ``Depends``
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import sys
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from fast_depends import Depends
|
||||
|
||||
|
||||
class CoefficientService:
|
||||
@staticmethod
|
||||
def get_coefficient() -> float:
|
||||
return 1.2
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
service = providers.Factory(CoefficientService)
|
||||
|
||||
|
||||
@inject
|
||||
def apply_coefficient(
|
||||
a: int,
|
||||
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
|
||||
) -> float:
|
||||
return a * coefficient_provider.get_coefficient()
|
||||
|
||||
|
||||
container = Container()
|
||||
container.wire(modules=[sys.modules[__name__]])
|
||||
|
||||
apply_coefficient(100) == 120.0
|
||||
|
||||
|
|
@ -20,5 +20,6 @@ scipy
|
|||
boto3
|
||||
mypy_boto3_s3
|
||||
typing_extensions
|
||||
fast-depends
|
||||
|
||||
-r requirements-ext.txt
|
||||
|
|
|
@ -58,11 +58,19 @@ else:
|
|||
def get_origin(tp):
|
||||
return None
|
||||
|
||||
MARKER_EXTRACTORS = []
|
||||
|
||||
try:
|
||||
import fastapi.params
|
||||
from fastapi.params import Depends as FastApiDepends
|
||||
except ImportError:
|
||||
fastapi = None
|
||||
pass
|
||||
else:
|
||||
def extract_marker_from_fastapi(param: Any) -> Any:
|
||||
if isinstance(param, FastApiDepends):
|
||||
return param.dependency
|
||||
return None
|
||||
|
||||
MARKER_EXTRACTORS.append(extract_marker_from_fastapi)
|
||||
|
||||
|
||||
try:
|
||||
|
@ -77,6 +85,18 @@ except ImportError:
|
|||
werkzeug = None
|
||||
|
||||
|
||||
try:
|
||||
from fast_depends.dependencies import Depends as FastDepends
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
def extract_marker_from_fast_depends(param: Any) -> Any:
|
||||
if isinstance(param, FastDepends):
|
||||
return param.dependency
|
||||
return None
|
||||
|
||||
MARKER_EXTRACTORS.append(extract_marker_from_fast_depends)
|
||||
|
||||
from . import providers
|
||||
|
||||
__all__ = (
|
||||
|
@ -607,15 +627,14 @@ def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
|
|||
else:
|
||||
marker = parameter.default
|
||||
|
||||
if not isinstance(marker, _Marker) and not _is_fastapi_depends(marker):
|
||||
for marker_extractor in MARKER_EXTRACTORS:
|
||||
if _marker := marker_extractor(marker):
|
||||
marker = _marker
|
||||
break
|
||||
|
||||
if not isinstance(marker, _Marker):
|
||||
return None
|
||||
|
||||
if _is_fastapi_depends(marker):
|
||||
marker = marker.dependency
|
||||
|
||||
if not isinstance(marker, _Marker):
|
||||
return None
|
||||
|
||||
return marker
|
||||
|
||||
|
||||
|
@ -735,10 +754,6 @@ def _get_patched(
|
|||
return patched
|
||||
|
||||
|
||||
def _is_fastapi_depends(param: Any) -> bool:
|
||||
return fastapi and isinstance(param, fastapi.params.Depends)
|
||||
|
||||
|
||||
def _is_patched(fn) -> bool:
|
||||
return _patched_registry.has_callable(fn)
|
||||
|
||||
|
|
36
tests/unit/samples/wiringfastdepends/sample.py
Normal file
36
tests/unit/samples/wiringfastdepends/sample.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import sys
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import inject, Provide
|
||||
from fast_depends import Depends
|
||||
from typing_extensions import Annotated
|
||||
|
||||
|
||||
class CoefficientService:
|
||||
@staticmethod
|
||||
def get_coefficient() -> float:
|
||||
return 1.2
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
service = providers.Factory(CoefficientService)
|
||||
|
||||
|
||||
@inject
|
||||
def apply_coefficient(
|
||||
a: int,
|
||||
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
|
||||
) -> float:
|
||||
return a * coefficient_provider.get_coefficient()
|
||||
|
||||
|
||||
@inject
|
||||
def apply_coefficient_annotated(
|
||||
a: int,
|
||||
coefficient_provider: Annotated[CoefficientService, Depends(Provide[Container.service])],
|
||||
) -> float:
|
||||
return a * coefficient_provider.get_coefficient()
|
||||
|
||||
|
||||
container = Container()
|
||||
container.wire(modules=[sys.modules[__name__]])
|
11
tests/unit/wiring/test_fastdepends.py
Normal file
11
tests/unit/wiring/test_fastdepends.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
from dependency_injector.wiring import inject, Provide
|
||||
|
||||
from wiringfastdepends import sample
|
||||
|
||||
|
||||
def test_apply_coefficient() -> None:
|
||||
assert sample.apply_coefficient(100) == 120.0
|
||||
|
||||
|
||||
def test_apply_coefficient_annotated() -> None:
|
||||
assert sample.apply_coefficient_annotated(100) == 120.0
|
Loading…
Reference in New Issue
Block a user