FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-138151: Fix `ForwardRef.evaluate()` for locally defined generics by dr-carlos · Pull Request #138161 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 7 additions & 0 deletions Lib/annotationlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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])
Comment thread
dr-carlos marked this conversation as resolved.

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL