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 ,
2427 Union ,
2528 cast ,
2629)
30+ from warnings import warn
2731
2832try :
2933 from typing import Self
@@ -59,13 +63,11 @@ def get_origin(tp):
5963 return None
6064
6165
62- MARKER_EXTRACTORS = []
66+ MARKER_EXTRACTORS : List [Callable [[Any ], Any ]] = []
67+ INSPECT_EXCLUSION_FILTERS : List [Callable [[Any ], bool ]] = [isbuiltin ]
6368
64- try :
69+ with suppress ( ImportError ) :
6570 from fastapi .params import Depends as FastAPIDepends
66- except ImportError :
67- pass
68- else :
6971
7072 def extract_marker_from_fastapi (param : Any ) -> Any :
7173 if isinstance (param , FastAPIDepends ):
@@ -74,11 +76,8 @@ def extract_marker_from_fastapi(param: Any) -> Any:
7476
7577 MARKER_EXTRACTORS .append (extract_marker_from_fastapi )
7678
77- try :
79+ with suppress ( ImportError ) :
7880 from fast_depends .dependencies import Depends as FastDepends
79- except ImportError :
80- pass
81- else :
8281
8382 def extract_marker_from_fast_depends (param : Any ) -> Any :
8483 if isinstance (param , FastDepends ):
@@ -88,16 +87,22 @@ def extract_marker_from_fast_depends(param: Any) -> Any:
8887 MARKER_EXTRACTORS .append (extract_marker_from_fast_depends )
8988
9089
91- try :
92- import starlette .requests
93- except ImportError :
94- starlette = None
90+ with suppress (ImportError ):
91+ from starlette .requests import Request as StarletteRequest
9592
93+ def is_starlette_request_cls (obj : Any ) -> bool :
94+ return isclass (obj ) and _safe_is_subclass (obj , StarletteRequest )
9695
97- try :
98- import werkzeug .local
99- except ImportError :
100- 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 )
101106
102107from . import providers # noqa: E402
103108
@@ -130,6 +135,10 @@ def extract_marker_from_fast_depends(param: Any) -> Any:
130135 Container = Any
131136
132137
138+ class DIWiringWarning (RuntimeWarning ):
139+ """Base class for all warnings raised by the wiring module."""
140+
141+
133142class PatchedRegistry :
134143
135144 def __init__ (self ) -> None :
@@ -411,30 +420,11 @@ def _create_providers_map(
411420 return providers_map
412421
413422
414- class InspectFilter :
415-
416- def is_excluded (self , instance : object ) -> bool :
417- if self ._is_werkzeug_local_proxy (instance ):
418- return True
419- elif self ._is_starlette_request_cls (instance ):
423+ def is_excluded_from_inspect (obj : Any ) -> bool :
424+ for is_excluded in INSPECT_EXCLUSION_FILTERS :
425+ if is_excluded (obj ):
420426 return True
421- elif self ._is_builtin (instance ):
422- return True
423- else :
424- return False
425-
426- def _is_werkzeug_local_proxy (self , instance : object ) -> bool :
427- return werkzeug and isinstance (instance , werkzeug .local .LocalProxy )
428-
429- def _is_starlette_request_cls (self , instance : object ) -> bool :
430- return (
431- starlette
432- and isinstance (instance , type )
433- and _safe_is_subclass (instance , starlette .requests .Request )
434- )
435-
436- def _is_builtin (self , instance : object ) -> bool :
437- return inspect .isbuiltin (instance )
427+ return False
438428
439429
440430def wire ( # noqa: C901
@@ -455,7 +445,7 @@ def wire( # noqa: C901
455445
456446 for module in modules :
457447 for member_name , member in _get_members_and_annotated (module ):
458- if _inspect_filter . is_excluded (member ):
448+ if is_excluded_from_inspect (member ):
459449 continue
460450
461451 if _is_marker (member ):
@@ -520,6 +510,11 @@ def unwire( # noqa: C901
520510def inject (fn : F ) -> F :
521511 """Decorate callable with injecting decorator."""
522512 reference_injections , reference_closing = _fetch_reference_injections (fn )
513+
514+ if not reference_injections :
515+ warn ("@inject is not required here" , DIWiringWarning , stacklevel = 2 )
516+ return fn
517+
523518 patched = _get_patched (fn , reference_injections , reference_closing )
524519 return cast (F , patched )
525520
@@ -1054,7 +1049,6 @@ def is_loader_installed() -> bool:
10541049
10551050
10561051_patched_registry = PatchedRegistry ()
1057- _inspect_filter = InspectFilter ()
10581052_loader = AutoLoader ()
10591053
10601054# Optimizations
0 commit comments