Skip to content

Commit 9905ea9

Browse files
authored
FIX: don't evaluate annotations when collecting signature parameters (Python 3.14 / PEP 649) (#700)
* fix evaluation of annotations * add test
1 parent d60e24d commit 9905ea9

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎numpydoc/tests/test_validate.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import warnings
23
from contextlib import nullcontext
34
from dataclasses import dataclass
@@ -57,6 +58,18 @@ def test_no_file():
5758
validate.validate("numpydoc.tests.test_validate._DummyList.clear")
5859

5960

61+
@pytest.mark.skipif(sys.version_info < (3, 14), reason="PEP 649 lazy annotations")
62+
def test_signature_params_with_typechecking_only_annotation():
63+
ns = {}
64+
exec("def func(a, b=1, *args, **kwargs) -> OnlyUnderTypeChecking: ...", ns)
65+
assert Validator(get_doc_object(ns["func"])).signature_parameters == (
66+
"a",
67+
"b",
68+
"*args",
69+
"**kwargs",
70+
)
71+
72+
6073
@pytest.mark.parametrize(
6174
["file_contents", "expected"],
6275
[

‎numpydoc/validate.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import pydoc
1616
import re
17+
import sys
1718
import textwrap
1819
import tokenize
1920
from copy import deepcopy
@@ -435,7 +436,19 @@ def add_stars(param_name, info):
435436
# accessor classes have a signature but don't want to show this
436437
return tuple()
437438
try:
438-
sig = inspect.signature(self.obj)
439+
if sys.version_info >= (3, 14):
440+
# PEP 649: on Python 3.14+ ``inspect.signature`` evaluates annotations
441+
# eagerly, raising ``NameError`` for forward references that only
442+
# resolve under ``TYPE_CHECKING``. Only parameter names and kinds are
443+
# used below, so request the forward-ref-preserving format to
444+
# introspect the signature without evaluating annotations.
445+
import annotationlib
446+
447+
sig = inspect.signature(
448+
self.obj, annotation_format=annotationlib.Format.FORWARDREF
449+
)
450+
else:
451+
sig = inspect.signature(self.obj)
439452
except (TypeError, ValueError):
440453
# Some objects, mainly in C extensions do not support introspection
441454
# of the signature

0 commit comments

Comments
 (0)