@@ -591,9 +591,18 @@ def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
591
591
return marker
592
592
593
593
594
- def _fetch_reference_injections ( # noqa: C901
594
+ _signature_cache : Dict [int , Tuple [Dict [str , Any ], Dict [str , Any ]]] = {}
595
+
596
+
597
+ def _fetch_reference_injections (
595
598
fn : Callable [..., Any ],
596
599
) -> Tuple [Dict [str , Any ], Dict [str , Any ]]:
600
+ """Get reference injections with caching."""
601
+ # Use the function's id as cache key
602
+ fn_id = id (fn )
603
+ if fn_id in _signature_cache :
604
+ return _signature_cache [fn_id ]
605
+
597
606
# Hotfix, see:
598
607
# - https://github.com/ets-labs/python-dependency-injector/issues/362
599
608
# - https://github.com/ets-labs/python-dependency-injector/issues/398
@@ -606,9 +615,13 @@ def _fetch_reference_injections( # noqa: C901
606
615
signature = inspect .signature (fn )
607
616
except ValueError as exception :
608
617
if "no signature found" in str (exception ):
609
- return {}, {}
618
+ result = {}, {}
619
+ _signature_cache [fn_id ] = result
620
+ return result
610
621
elif "not supported by signature" in str (exception ):
611
- return {}, {}
622
+ result = {}, {}
623
+ _signature_cache [fn_id ] = result
624
+ return result
612
625
else :
613
626
raise exception
614
627
@@ -625,7 +638,11 @@ def _fetch_reference_injections( # noqa: C901
625
638
closing [parameter_name ] = marker
626
639
627
640
injections [parameter_name ] = marker
628
- return injections , closing
641
+
642
+ # Cache the result
643
+ result = (injections , closing )
644
+ _signature_cache [fn_id ] = result
645
+ return result
629
646
630
647
631
648
def _locate_dependent_closing_args (
@@ -1036,4 +1053,5 @@ def _patched(*args, **kwargs):
1036
1053
patched .injections ,
1037
1054
patched .closing ,
1038
1055
)
1056
+
1039
1057
return cast (F , _patched )
0 commit comments