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

[3.14] gh-137969: Fix evaluation of `ref.evaluate(format=Format.FORWARDREF)` objects (GH-138075) by miss-islington · Pull Request #140929 · python/cpython · GitHub

/ cpython Public
Merged
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
14 changes: 8 additions & 6 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 @@ -159,19 +159,21 @@ def evaluate(
type_params = getattr(owner, "__type_params__", None)

# Type parameters exist in their own scope, which is logically
# between the locals and the globals. We simulate this by adding
# them to the globals.
# between the locals and the globals.
type_param_scope = {}
if type_params is not None:
globals = dict(globals)
for param in type_params:
globals[param.__name__] = param
type_param_scope[param.__name__] = param

if self.__extra_names__:
locals = {**locals, **self.__extra_names__}

arg = self.__forward_arg__
if arg.isidentifier() and not keyword.iskeyword(arg):
if arg in locals:
return locals[arg]
elif arg in type_param_scope:
return type_param_scope[arg]
elif arg in globals:
return globals[arg]
elif hasattr(builtins, arg):
Expand All @@ -183,15 +185,15 @@ def evaluate(
else:
code = self.__forward_code__
try:
return eval(code, globals=globals, locals=locals)
return eval(code, globals=globals, locals={**type_param_scope, **locals})
except Exception:
if not is_forwardref_format:
raise

# All variables, in scoping order, should be checked before
# triggering __missing__ to create a _Stringifier.
new_locals = _StringifierDict(
{**builtins.__dict__, **globals, **locals},
{**builtins.__dict__, **globals, **type_param_scope, **locals},
globals=globals,
owner=owner,
is_class=self.__forward_is_class__,
Expand Down
9 changes: 9 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 @@ -1911,6 +1911,15 @@ def test_fwdref_invalid_syntax(self):
with self.assertRaises(SyntaxError):
fr.evaluate()

def test_re_evaluate_generics(self):
global alias
class C:
x: alias[int]

evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
alias = list
self.assertEqual(evaluated.evaluate(), list[int])


class TestAnnotationLib(unittest.TestCase):
def test__all__(self):
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` returning :class:`annotationlib.ForwardRef`
objects which do not update in new contexts.
Loading

Back | FazBrowse Home | New Git URL