Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c58064

Browse files
committed
Make wiring inspect exclsuions extensible
1 parent eb74b1e commit 0c58064

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

‎src/dependency_injector/wiring.py

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import inspect
77
import pkgutil
88
import sys
9+
from contextlib import suppress
10+
from inspect import isbuiltin, isclass
911
from types import ModuleType
1012
from typing import (
1113
TYPE_CHECKING,
@@ -15,6 +17,7 @@
1517
Dict,
1618
Iterable,
1719
Iterator,
20+
List,
1821
Optional,
1922
Protocol,
2023
Set,
@@ -60,13 +63,11 @@ def get_origin(tp):
6063
return None
6164

6265

63-
MARKER_EXTRACTORS = []
66+
MARKER_EXTRACTORS: List[Callable[[Any], Any]] = []
67+
INSPECT_EXCLUSION_FILTERS: List[Callable[[Any], bool]] = [isbuiltin]
6468

65-
try:
69+
withsuppress(ImportError):
6670
from fastapi.params import Depends as FastAPIDepends
67-
except ImportError:
68-
pass
69-
else:
7071

7172
def extract_marker_from_fastapi(param: Any) -> Any:
7273
if isinstance(param, FastAPIDepends):
@@ -75,11 +76,8 @@ def extract_marker_from_fastapi(param: Any) -> Any:
7576

7677
MARKER_EXTRACTORS.append(extract_marker_from_fastapi)
7778

78-
try:
79+
withsuppress(ImportError):
7980
from fast_depends.dependencies import Depends as FastDepends
80-
except ImportError:
81-
pass
82-
else:
8381

8482
def extract_marker_from_fast_depends(param: Any) -> Any:
8583
if isinstance(param, FastDepends):
@@ -89,16 +87,22 @@ def extract_marker_from_fast_depends(param: Any) -> Any:
8987
MARKER_EXTRACTORS.append(extract_marker_from_fast_depends)
9088

9189

92-
try:
93-
import starlette.requests
94-
except ImportError:
95-
starlette = None
90+
with suppress(ImportError):
91+
from starlette.requests import Request as StarletteRequest
9692

93+
def is_starlette_request_cls(obj: Any) -> bool:
94+
return isclass(obj) and _safe_is_subclass(obj, StarletteRequest)
9795

98-
try:
99-
import werkzeug.local
100-
except ImportError:
101-
werkzeug = None
96+
INSPECT_EXCLUSION_FILTERS.append(is_starlette_request_cls)
97+
98+
99+
with suppress(ImportError):
100+
from werkzeug.local import LocalProxy as WerkzeugLocalProxy
101+
102+
def is_werkzeug_local_proxy(obj: Any) -> bool:
103+
return isinstance(obj, WerkzeugLocalProxy)
104+
105+
INSPECT_EXCLUSION_FILTERS.append(is_werkzeug_local_proxy)
102106

103107
from . import providers # noqa: E402
104108

@@ -416,30 +420,11 @@ def _create_providers_map(
416420
return providers_map
417421

418422

419-
class InspectFilter:
420-
421-
def is_excluded(self, instance: object) -> bool:
422-
if self._is_werkzeug_local_proxy(instance):
423+
def is_excluded_from_inspect(obj: Any) -> bool:
424+
for is_excluded in INSPECT_EXCLUSION_FILTERS:
425+
if is_excluded(obj):
423426
return True
424-
elif self._is_starlette_request_cls(instance):
425-
return True
426-
elif self._is_builtin(instance):
427-
return True
428-
else:
429-
return False
430-
431-
def _is_werkzeug_local_proxy(self, instance: object) -> bool:
432-
return werkzeug and isinstance(instance, werkzeug.local.LocalProxy)
433-
434-
def _is_starlette_request_cls(self, instance: object) -> bool:
435-
return (
436-
starlette
437-
and isinstance(instance, type)
438-
and _safe_is_subclass(instance, starlette.requests.Request)
439-
)
440-
441-
def _is_builtin(self, instance: object) -> bool:
442-
return inspect.isbuiltin(instance)
427+
return False
443428

444429

445430
def wire( # noqa: C901
@@ -460,7 +445,7 @@ def wire( # noqa: C901
460445

461446
for module in modules:
462447
for member_name, member in _get_members_and_annotated(module):
463-
if _inspect_filter.is_excluded(member):
448+
if is_excluded_from_inspect(member):
464449
continue
465450

466451
if _is_marker(member):
@@ -1064,7 +1049,6 @@ def is_loader_installed() -> bool:
10641049

10651050

10661051
_patched_registry = PatchedRegistry()
1067-
_inspect_filter = InspectFilter()
10681052
_loader = AutoLoader()
10691053

10701054
# Optimizations

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /