Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1d251b8

Browse files
gh-128184: Fix display of signatures with ForwardRefs (#130815)
Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent80e6d3e commit1d251b8

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

‎Lib/dataclasses.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
11631163
try:
11641164
# In some cases fetching a signature is not possible.
11651165
# But, we surely should not fail in this case.
1166-
text_sig=str(inspect.signature(cls)).replace(' -> None','')
1166+
text_sig=str(inspect.signature(
1167+
cls,
1168+
annotation_format=annotationlib.Format.FORWARDREF,
1169+
)).replace(' -> None','')
11671170
except (TypeError,ValueError):
11681171
text_sig=''
11691172
cls.__doc__= (cls.__name__+text_sig)

‎Lib/inspect.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143

144144

145145
importabc
146-
fromannotationlibimportFormat
146+
fromannotationlibimportFormat,ForwardRef
147147
fromannotationlibimportget_annotations# re-exported
148148
importast
149149
importdis
@@ -1342,6 +1342,8 @@ def repl(match):
13421342
ifannotation.__module__in ('builtins',base_module):
13431343
returnannotation.__qualname__
13441344
returnannotation.__module__+'.'+annotation.__qualname__
1345+
ifisinstance(annotation,ForwardRef):
1346+
returnannotation.__forward_arg__
13451347
returnrepr(annotation)
13461348

13471349
defformatannotationrelativeto(object):

‎Lib/test/test_dataclasses/__init__.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
importtypes
1313
importweakref
1414
importtraceback
15+
importtextwrap
1516
importunittest
1617
fromunittest.mockimportMock
1718
fromtypingimportClassVar,Any,List,Union,Tuple,Dict,Generic,TypeVar,Optional,Protocol,DefaultDict
@@ -2343,6 +2344,31 @@ class C:
23432344

23442345
self.assertDocStrEqual(C.__doc__,"C(x:collections.deque=<factory>)")
23452346

2347+
deftest_docstring_undefined_name(self):
2348+
@dataclass
2349+
classC:
2350+
x:undef
2351+
2352+
self.assertDocStrEqual(C.__doc__,"C(x:undef)")
2353+
2354+
deftest_docstring_with_unsolvable_forward_ref_in_init(self):
2355+
# See: https://github.com/python/cpython/issues/128184
2356+
ns= {}
2357+
exec(
2358+
textwrap.dedent(
2359+
"""
2360+
from dataclasses import dataclass
2361+
2362+
@dataclass
2363+
class C:
2364+
def __init__(self, x: X, num: int) -> None: ...
2365+
""",
2366+
),
2367+
ns,
2368+
)
2369+
2370+
self.assertDocStrEqual(ns['C'].__doc__,"C(x:X,num:int)")
2371+
23462372
deftest_docstring_with_no_signature(self):
23472373
# See https://github.com/python/cpython/issues/103449
23482374
classMeta(type):

‎Lib/test/test_inspect/test_inspect.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,10 @@ def test_typing_replacement(self):
17531753
self.assertEqual(inspect.formatannotation(ann),'Union[List[str], int]')
17541754
self.assertEqual(inspect.formatannotation(ann1),'Union[List[testModule.typing.A], int]')
17551755

1756+
deftest_forwardref(self):
1757+
fwdref=ForwardRef('fwdref')
1758+
self.assertEqual(inspect.formatannotation(fwdref),'fwdref')
1759+
17561760

17571761
classTestIsMethodDescriptor(unittest.TestCase):
17581762

@@ -4587,6 +4591,11 @@ def foo(a: list[str]) -> Tuple[str, float]:
45874591
self.assertEqual(str(inspect.signature(foo)),
45884592
inspect.signature(foo).format())
45894593

4594+
deffoo(x:undef):
4595+
pass
4596+
sig=inspect.signature(foo,annotation_format=Format.FORWARDREF)
4597+
self.assertEqual(str(sig),'(x: undef)')
4598+
45904599
deftest_signature_str_positional_only(self):
45914600
P=inspect.Parameter
45924601
S=inspect.Signature
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve display of:class:`annotationlib.ForwardRef` object
2+
within:class:`inspect.Signature` representations.
3+
This also fixes a:exc:`NameError` that was raised when using
4+
:func:`dataclasses.dataclass` on classes with unresolvable forward references.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp