Skip to content

Commit 4b97939

Browse files
committed
Use FORWARDREF format in inspect.signature calls
Fixes #13945
1 parent ad3f3cc commit 4b97939

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

sphinx/util/inspect.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,26 @@ def signature(
716716
if type_aliases is None:
717717
type_aliases = {}
718718

719+
# Python 3.14 added the annotationlib module to the standard library as well as the
720+
# 'annotation_format' keyword parameter to inspect.signature(), which allows us to handle
721+
# forward references more robustly.
722+
try:
723+
import annotationlib
724+
725+
inspect_signature_extra = {'annotation_format': annotationlib.Format.FORWARDREF}
726+
except ImportError:
727+
inspect_signature_extra = {}
728+
719729
try:
720730
if _should_unwrap(subject):
721-
signature = inspect.signature(subject) # type: ignore[arg-type]
731+
signature = inspect.signature(subject, **inspect_signature_extra) # type: ignore[arg-type]
722732
else:
723-
signature = inspect.signature(subject, follow_wrapped=True) # type: ignore[arg-type]
733+
signature = inspect.signature(
734+
subject, follow_wrapped=True, **inspect_signature_extra
735+
) # type: ignore[arg-type]
724736
except ValueError:
725737
# follow built-in wrappers up (ex. functools.lru_cache)
726-
signature = inspect.signature(subject) # type: ignore[arg-type]
738+
signature = inspect.signature(subject, **inspect_signature_extra) # type: ignore[arg-type]
727739
parameters = list(signature.parameters.values())
728740
return_annotation = signature.return_annotation
729741

0 commit comments

Comments
 (0)