@@ -72,6 +72,18 @@ def __call__(self, obj: Any, name: str, default: Any = ..., /) -> Any: ...
7272isclass = inspect .isclass
7373ismodule = inspect .ismodule
7474
75+ # Python 3.14 added the annotationlib module to the standard library as well as the
76+ # 'annotation_format' keyword parameter to inspect.signature(), which allows us to handle
77+ # forward references more robustly.
78+ if sys .version_info [0 :2 ] >= (3 , 14 ):
79+ import annotationlib # type: ignore[import-not-found]
80+
81+ inspect_signature_extra : dict [str , Any ] = {
82+ 'annotation_format' : annotationlib .Format .FORWARDREF
83+ }
84+ else :
85+ inspect_signature_extra : dict [str , Any ] = {}
86+
7587
7688def unwrap (obj : Any ) -> Any :
7789 """Get an original object from wrapped object (wrapped functions).
@@ -718,12 +730,16 @@ def signature(
718730
719731 try :
720732 if _should_unwrap (subject ):
721- signature = inspect .signature (subject ) # type: ignore[arg-type]
733+ signature = inspect .signature (subject , ** inspect_signature_extra ) # type: ignore[arg-type]
722734 else :
723- signature = inspect .signature (subject , follow_wrapped = True ) # type: ignore[arg-type]
735+ signature = inspect .signature (
736+ subject , # type: ignore[arg-type]
737+ follow_wrapped = True ,
738+ ** inspect_signature_extra ,
739+ )
724740 except ValueError :
725741 # follow built-in wrappers up (ex. functools.lru_cache)
726- signature = inspect .signature (subject ) # type: ignore[arg-type]
742+ signature = inspect .signature (subject , ** inspect_signature_extra ) # type: ignore[arg-type]
727743 parameters = list (signature .parameters .values ())
728744 return_annotation = signature .return_annotation
729745
0 commit comments