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

View File

@ -58,7 +58,6 @@ except ImportError:
from . import providers from . import providers
if sys.version_info[:2] == (3, 5): if sys.version_info[:2] == (3, 5):
warnings.warn( warnings.warn(
"Dependency Injector will drop support of Python 3.5 after Jan 1st of 2022. " "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 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): def _get_async_patched(fn):
@functools.wraps(fn) @functools.wraps(fn)
async def _patched(*args, **kwargs): async def _patched(*args, **kwargs):
@ -962,3 +936,6 @@ def is_loader_installed() -> bool:
_patched_registry = PatchedRegistry() _patched_registry = PatchedRegistry()
_inspect_filter = InspectFilter() _inspect_filter = InspectFilter()
_loader = AutoLoader() _loader = AutoLoader()
# Optimizations
from ._cwiring import _get_sync_patched # noqa