66import inspect
77import pkgutil
88import sys
9+ from contextlib import suppress
10+ from inspect import isbuiltin , isclass
911from types import ModuleType
1012from typing import (
1113 TYPE_CHECKING ,
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+ with suppress ( 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+ with suppress ( 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
103107from . 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
445430def 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