Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ def evaluate(
globals[param.__name__] = param
if self.__extra_names__:
locals = {**locals, **self.__extra_names__}
if (annotate := getattr(owner, "__annotate__", None)) and annotate.__closure__:
for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__):
if name != "__classdict__":
try:
locals[name] = cell.cell_contents
except ValueError:
pass

arg = self.__forward_arg__
if arg.isidentifier() and not keyword.iskeyword(arg):
Expand Down
27 changes: 27 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,33 @@ def test_evaluate_with_type_params_and_scope_conflict(self):
TypeParamsSample.TypeParamsAlias2,
)

def test_evaluate_local_generic(self):
class Cls:
nonlocal alias, alias2, alias3
x: alias[int]
y: alias[alias]
z: alias[alias2, alias3]

fwdref = get_annotations(Cls, format=Format.FORWARDREF)["x"]
with self.assertRaises(NameError):
fwdref.evaluate()

alias = list
self.assertEqual(fwdref.evaluate(), list[int])

del alias
fwdref = get_annotations(Cls, format=Format.FORWARDREF)["y"]
alias = list
self.assertEqual(fwdref.evaluate(), list[list])

del alias
fwdref = get_annotations(Cls, format=Format.FORWARDREF)["z"]
alias = dict
alias2 = int
alias3 = str
self.assertEqual(fwdref.evaluate(), dict[int, str])


def test_fwdref_with_module(self):
self.assertIs(ForwardRef("Format", module="annotationlib").evaluate(), Format)
self.assertIs(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`annotationlib.ForwardRef.evaluate` not handling generics with
nonlocal variables that are defined after the annotation.
Loading