Skip to content

Commit d3be6f9

Browse files
gh-125614: annotationlib: Fix bug where not all Stringifiers are converted (#125635)
1 parent 8f2c0f7 commit d3be6f9

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

Lib/annotationlib.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Format(enum.IntEnum):
4545
"__globals__",
4646
"__owner__",
4747
"__cell__",
48+
"__stringifier_dict__",
4849
)
4950

5051

@@ -268,7 +269,16 @@ class _Stringifier:
268269
# instance of the other in place.
269270
__slots__ = _SLOTS
270271

271-
def __init__(self, node, globals=None, owner=None, is_class=False, cell=None):
272+
def __init__(
273+
self,
274+
node,
275+
globals=None,
276+
owner=None,
277+
is_class=False,
278+
cell=None,
279+
*,
280+
stringifier_dict,
281+
):
272282
# Either an AST node or a simple str (for the common case where a ForwardRef
273283
# represent a single name).
274284
assert isinstance(node, (ast.AST, str))
@@ -283,6 +293,7 @@ def __init__(self, node, globals=None, owner=None, is_class=False, cell=None):
283293
self.__globals__ = globals
284294
self.__cell__ = cell
285295
self.__owner__ = owner
296+
self.__stringifier_dict__ = stringifier_dict
286297

287298
def __convert_to_ast(self, other):
288299
if isinstance(other, _Stringifier):
@@ -317,9 +328,15 @@ def __get_ast(self):
317328
return node
318329

319330
def __make_new(self, node):
320-
return _Stringifier(
321-
node, self.__globals__, self.__owner__, self.__forward_is_class__
331+
stringifier = _Stringifier(
332+
node,
333+
self.__globals__,
334+
self.__owner__,
335+
self.__forward_is_class__,
336+
stringifier_dict=self.__stringifier_dict__,
322337
)
338+
self.__stringifier_dict__.stringifiers.append(stringifier)
339+
return stringifier
323340

324341
# Must implement this since we set __eq__. We hash by identity so that
325342
# stringifiers in dict keys are kept separate.
@@ -462,6 +479,7 @@ def __missing__(self, key):
462479
globals=self.globals,
463480
owner=self.owner,
464481
is_class=self.is_class,
482+
stringifier_dict=self,
465483
)
466484
self.stringifiers.append(fwdref)
467485
return fwdref
@@ -516,7 +534,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
516534
name = freevars[i]
517535
else:
518536
name = "__cell__"
519-
fwdref = _Stringifier(name)
537+
fwdref = _Stringifier(name, stringifier_dict=globals)
520538
new_closure.append(types.CellType(fwdref))
521539
closure = tuple(new_closure)
522540
else:
@@ -573,6 +591,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
573591
owner=owner,
574592
globals=annotate.__globals__,
575593
is_class=is_class,
594+
stringifier_dict=globals,
576595
)
577596
globals.stringifiers.append(fwdref)
578597
new_closure.append(types.CellType(fwdref))
@@ -591,6 +610,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
591610
result = func(Format.VALUE)
592611
for obj in globals.stringifiers:
593612
obj.__class__ = ForwardRef
613+
obj.__stringifier_dict__ = None # not needed for ForwardRef
594614
if isinstance(obj.__ast_node__, str):
595615
obj.__arg__ = obj.__ast_node__
596616
obj.__ast_node__ = None

Lib/test/test_annotationlib.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,42 @@ def f(x: int, y: doesntexist):
8080
fwdref.evaluate()
8181
self.assertEqual(fwdref.evaluate(globals={"doesntexist": 1}), 1)
8282

83+
def test_nonexistent_attribute(self):
84+
def f(
85+
x: some.module,
86+
y: some[module],
87+
z: some(module),
88+
alpha: some | obj,
89+
beta: +some,
90+
gamma: some < obj,
91+
):
92+
pass
93+
94+
anno = annotationlib.get_annotations(f, format=Format.FORWARDREF)
95+
x_anno = anno["x"]
96+
self.assertIsInstance(x_anno, ForwardRef)
97+
self.assertEqual(x_anno, ForwardRef("some.module"))
98+
99+
y_anno = anno["y"]
100+
self.assertIsInstance(y_anno, ForwardRef)
101+
self.assertEqual(y_anno, ForwardRef("some[module]"))
102+
103+
z_anno = anno["z"]
104+
self.assertIsInstance(z_anno, ForwardRef)
105+
self.assertEqual(z_anno, ForwardRef("some(module)"))
106+
107+
alpha_anno = anno["alpha"]
108+
self.assertIsInstance(alpha_anno, ForwardRef)
109+
self.assertEqual(alpha_anno, ForwardRef("some | obj"))
110+
111+
beta_anno = anno["beta"]
112+
self.assertIsInstance(beta_anno, ForwardRef)
113+
self.assertEqual(beta_anno, ForwardRef("+some"))
114+
115+
gamma_anno = anno["gamma"]
116+
self.assertIsInstance(gamma_anno, ForwardRef)
117+
self.assertEqual(gamma_anno, ForwardRef("some < obj"))
118+
83119

84120
class TestSourceFormat(unittest.TestCase):
85121
def test_closure(self):
@@ -91,6 +127,16 @@ def inner(arg: x):
91127
anno = annotationlib.get_annotations(inner, format=Format.STRING)
92128
self.assertEqual(anno, {"arg": "x"})
93129

130+
def test_closure_undefined(self):
131+
if False:
132+
x = 0
133+
134+
def inner(arg: x):
135+
pass
136+
137+
anno = annotationlib.get_annotations(inner, format=Format.STRING)
138+
self.assertEqual(anno, {"arg": "x"})
139+
94140
def test_function(self):
95141
def f(x: int, y: doesntexist):
96142
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In the :data:`~annotationlib.Format.FORWARDREF` format of
2+
:mod:`annotationlib`, fix bug where nested expressions were not returned as
3+
:class:`annotationlib.ForwardRef` format.

0 commit comments

Comments
 (0)