Wiring changes

This commit is contained in:
Roman Mogylatov 2022-03-27 20:00:32 -04:00
parent b4fb9a7178
commit 09cf3459c5
3 changed files with 457 additions and 542 deletions

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@ import sys
import types
from . import providers
from .wiring import _Marker
if sys.version_info[0] == 3: # pragma: no cover
@ -22,7 +23,10 @@ else: # pragma: no cover
def _get_sync_patched(fn):
@functools.wraps(fn)
def _patched(*args, **kwargs):
cdef dict to_inject = kwargs.copy()
cdef object result
cdef dict to_inject
to_inject = kwargs.copy()
for injection, provider in _patched.__injections__.items():
if injection not in kwargs \
or _is_fastapi_default_arg_injection(injection, kwargs):
@ -30,6 +34,7 @@ def _get_sync_patched(fn):
result = fn(*args, **to_inject)
if _patched.__closing__:
for injection, provider in _patched.__closing__.items():
if injection in kwargs \
and not _is_fastapi_default_arg_injection(injection, kwargs):
@ -44,8 +49,4 @@ def _get_sync_patched(fn):
cdef bint _is_fastapi_default_arg_injection(object injection, dict kwargs):
"""Check if injection is FastAPI injection of the default argument."""
return injection in kwargs and _is_marker(kwargs[injection])
cdef bint _is_marker(object instance):
return getattr(instance, "__IS_MARKER__", False) is True
return injection in kwargs and isinstance(kwargs[injection], _Marker)

View File

@ -58,7 +58,6 @@ except ImportError:
from . import providers
if sys.version_info[:2] == (3, 5):
warnings.warn(
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. "
@ -600,31 +599,6 @@ def _get_patched(fn, reference_injections, reference_closing):
return patched
from ._cwiring import _get_sync_patched
# def _get_sync_patched(fn):
# @functools.wraps(fn)
# def _patched(*args, **kwargs):
# to_inject = kwargs.copy()
# for injection, provider in _patched.__injections__.items():
# if injection not in kwargs \
# or _is_fastapi_default_arg_injection(injection, kwargs):
# to_inject[injection] = provider()
#
# result = fn(*args, **to_inject)
#
# for injection, provider in _patched.__closing__.items():
# if injection in kwargs \
# and not _is_fastapi_default_arg_injection(injection, kwargs):
# continue
# if not isinstance(provider, providers.Resource):
# continue
# provider.shutdown()
#
# return result
# return _patched
def _get_async_patched(fn):
@functools.wraps(fn)
async def _patched(*args, **kwargs):
@ -962,3 +936,6 @@ def is_loader_installed() -> bool:
_patched_registry = PatchedRegistry()
_inspect_filter = InspectFilter()
_loader = AutoLoader()
# Optimizations
from ._cwiring import _get_sync_patched # noqa