Skip to content

Commit 6f2d40b

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

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

sphinx/util/inspect.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,28 @@ 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 # type: ignore[import-not-found]
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, # type: ignore[arg-type]
735+
follow_wrapped=True,
736+
**inspect_signature_extra,
737+
)
724738
except ValueError:
725739
# follow built-in wrappers up (ex. functools.lru_cache)
726-
signature = inspect.signature(subject) # type: ignore[arg-type]
740+
signature = inspect.signature(subject, **inspect_signature_extra) # type: ignore[arg-type]
727741
parameters = list(signature.parameters.values())
728742
return_annotation = signature.return_annotation
729743

0 commit comments

Comments
 (0)