mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-07 13:54:25 +03:00
Add support for Fast Stream Depends (#898)
This commit is contained in:
parent
f2da51e0d4
commit
b411807572
48
docs/examples/fastdepends.rst
Normal file
48
docs/examples/fastdepends.rst
Normal file
|
@ -0,0 +1,48 @@
|
|||
.. _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 demonstrates how to use ``Dependency Injector`` with `FastDepends <https://github.com/Lancetnik/FastDepends>`_, a lightweight dependency injection framework inspired by FastAPI's dependency system, but without the web framework components.
|
||||
|
||||
Basic Usage
|
||||
-----------
|
||||
|
||||
The integration between FastDepends and Dependency Injector is straightforward. Simply use Dependency Injector's ``Provide`` marker within FastDepends' ``Depends`` function:
|
||||
|
||||
.. 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
|
|
@ -22,5 +22,6 @@ Explore the examples to see the ``Dependency Injector`` in action.
|
|||
fastapi
|
||||
fastapi-redis
|
||||
fastapi-sqlalchemy
|
||||
fastdepends
|
||||
|
||||
.. disqus::
|
||||
|
|
|
@ -693,5 +693,6 @@ Take a look at other application examples:
|
|||
- :ref:`fastapi-example`
|
||||
- :ref:`fastapi-redis-example`
|
||||
- :ref:`fastapi-sqlalchemy-example`
|
||||
- :ref:`fastdepends-example`
|
||||
|
||||
.. disqus::
|
||||
|
|
|
@ -20,5 +20,6 @@ scipy
|
|||
boto3
|
||||
mypy_boto3_s3
|
||||
typing_extensions
|
||||
fast-depends
|
||||
|
||||
-r requirements-ext.txt
|
||||
|
|
|
@ -59,10 +59,33 @@ else:
|
|||
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:
|
||||
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)
|
||||
|
||||
|
||||
try:
|
||||
|
@ -76,8 +99,7 @@ try:
|
|||
except ImportError:
|
||||
werkzeug = None
|
||||
|
||||
|
||||
from . import providers
|
||||
from . import providers # noqa: E402
|
||||
|
||||
__all__ = (
|
||||
"wire",
|
||||
|
@ -607,15 +629,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 +756,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)
|
||||
|
||||
|
|
39
tests/unit/samples/wiringfastdepends/sample.py
Normal file
39
tests/unit/samples/wiringfastdepends/sample.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
import sys
|
||||
|
||||
from fast_depends import Depends
|
||||
from typing_extensions import Annotated
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import Provide, inject
|
||||
|
||||
|
||||
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__]])
|
9
tests/unit/wiring/test_fastdepends.py
Normal file
9
tests/unit/wiring/test_fastdepends.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
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