Add support for Fast Stream Depends (#898)

This commit is contained in:
AndrianEquestrian 2025-06-16 10:37:31 +03:00 committed by GitHub
parent f2da51e0d4
commit b411807572
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 133 additions and 15 deletions

View 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

View File

@ -22,5 +22,6 @@ Explore the examples to see the ``Dependency Injector`` in action.
fastapi fastapi
fastapi-redis fastapi-redis
fastapi-sqlalchemy fastapi-sqlalchemy
fastdepends
.. disqus:: .. disqus::

View File

@ -693,5 +693,6 @@ Take a look at other application examples:
- :ref:`fastapi-example` - :ref:`fastapi-example`
- :ref:`fastapi-redis-example` - :ref:`fastapi-redis-example`
- :ref:`fastapi-sqlalchemy-example` - :ref:`fastapi-sqlalchemy-example`
- :ref:`fastdepends-example`
.. disqus:: .. disqus::

View File

@ -20,5 +20,6 @@ scipy
boto3 boto3
mypy_boto3_s3 mypy_boto3_s3
typing_extensions typing_extensions
fast-depends
-r requirements-ext.txt -r requirements-ext.txt

View File

@ -59,10 +59,33 @@ else:
return None return None
MARKER_EXTRACTORS = []
try: try:
import fastapi.params from fastapi.params import Depends as FastAPIDepends
except ImportError: 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: try:
@ -76,8 +99,7 @@ try:
except ImportError: except ImportError:
werkzeug = None werkzeug = None
from . import providers # noqa: E402
from . import providers
__all__ = ( __all__ = (
"wire", "wire",
@ -607,11 +629,10 @@ def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
else: else:
marker = parameter.default marker = parameter.default
if not isinstance(marker, _Marker) and not _is_fastapi_depends(marker): for marker_extractor in MARKER_EXTRACTORS:
return None if _marker := marker_extractor(marker):
marker = _marker
if _is_fastapi_depends(marker): break
marker = marker.dependency
if not isinstance(marker, _Marker): if not isinstance(marker, _Marker):
return None return None
@ -735,10 +756,6 @@ def _get_patched(
return patched return patched
def _is_fastapi_depends(param: Any) -> bool:
return fastapi and isinstance(param, fastapi.params.Depends)
def _is_patched(fn) -> bool: def _is_patched(fn) -> bool:
return _patched_registry.has_callable(fn) return _patched_registry.has_callable(fn)

View 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__]])

View 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

View File

@ -17,6 +17,7 @@ deps=
mypy_boto3_s3 mypy_boto3_s3
pydantic-settings pydantic-settings
werkzeug werkzeug
fast-depends
extras= extras=
yaml yaml
commands = pytest commands = pytest
@ -44,6 +45,7 @@ deps =
boto3 boto3
mypy_boto3_s3 mypy_boto3_s3
werkzeug werkzeug
fast-depends
commands = pytest -m pydantic commands = pytest -m pydantic
[testenv:coveralls] [testenv:coveralls]