mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Improve FastAPI integration
This commit is contained in:
parent
262c035bc1
commit
d37ae8e7db
|
@ -7,6 +7,10 @@ that were made in every particular version.
|
||||||
From version 0.7.6 *Dependency Injector* framework strictly
|
From version 0.7.6 *Dependency Injector* framework strictly
|
||||||
follows `Semantic versioning`_
|
follows `Semantic versioning`_
|
||||||
|
|
||||||
|
Develop
|
||||||
|
-------
|
||||||
|
- Improve ``FastAPI`` integration: handle ``Depends(Provide[...])``.
|
||||||
|
|
||||||
4.4.0
|
4.4.0
|
||||||
-----
|
-----
|
||||||
- Add ``@inject`` decorator. It helps to fix a number of wiring bugs and make wiring be more resilient.
|
- Add ``@inject`` decorator. It helps to fix a number of wiring bugs and make wiring be more resilient.
|
||||||
|
|
|
@ -27,6 +27,13 @@ else:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
from fastapi.params import Depends as FastAPIDepends
|
||||||
|
fastapi_installed = True
|
||||||
|
except ImportError:
|
||||||
|
fastapi_installed = False
|
||||||
|
|
||||||
|
|
||||||
from . import providers
|
from . import providers
|
||||||
|
|
||||||
|
|
||||||
|
@ -320,10 +327,15 @@ def _fetch_reference_injections(
|
||||||
injections = {}
|
injections = {}
|
||||||
closing = {}
|
closing = {}
|
||||||
for parameter_name, parameter in signature.parameters.items():
|
for parameter_name, parameter in signature.parameters.items():
|
||||||
if not isinstance(parameter.default, _Marker):
|
if not isinstance(parameter.default, _Marker) \
|
||||||
|
and not _is_fastapi_depends(parameter.default):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
marker = parameter.default
|
marker = parameter.default
|
||||||
|
|
||||||
|
if _is_fastapi_depends(marker):
|
||||||
|
marker = marker.dependency
|
||||||
|
|
||||||
if isinstance(marker, Closing):
|
if isinstance(marker, Closing):
|
||||||
marker = marker.provider
|
marker = marker.provider
|
||||||
closing[parameter_name] = marker
|
closing[parameter_name] = marker
|
||||||
|
@ -435,6 +447,10 @@ def _is_fastapi_default_arg_injection(injection, kwargs):
|
||||||
return injection in kwargs and isinstance(kwargs[injection], _Marker)
|
return injection in kwargs and isinstance(kwargs[injection], _Marker)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_fastapi_depends(param: Any) -> bool:
|
||||||
|
return fastapi_installed and isinstance(param, FastAPIDepends)
|
||||||
|
|
||||||
|
|
||||||
def _is_patched(fn):
|
def _is_patched(fn):
|
||||||
return getattr(fn, '__wired__', False) is True
|
return getattr(fn, '__wired__', False) is True
|
||||||
|
|
||||||
|
@ -459,6 +475,9 @@ class _Marker(Generic[T], metaclass=ClassGetItemMeta):
|
||||||
def __class_getitem__(cls, item) -> T:
|
def __class_getitem__(cls, item) -> T:
|
||||||
return cls(item)
|
return cls(item)
|
||||||
|
|
||||||
|
def __call__(self) -> T:
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
class Provide(_Marker):
|
class Provide(_Marker):
|
||||||
...
|
...
|
||||||
|
|
Loading…
Reference in New Issue
Block a user