Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Sep 25, 2024
1 parent 4b1214e commit cebb78e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import builtins
import enum
import functools
import keyword
import sys
import types

Expand Down Expand Up @@ -156,10 +157,10 @@ def evaluate(self, *, globals=None, locals=None, type_params=None, owner=None):
locals.pop(param_name, None)

arg = self.__forward_arg__
if arg.isidentifier():
if arg.isidentifier() and not keyword.iskeyword(arg):
if arg in locals:
value = locals[arg]
elif globals is not None and arg in globals:
elif arg in globals:
value = globals[arg]
elif hasattr(builtins, arg):
return getattr(builtins, arg)
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,14 @@ def test_name_lookup_without_eval(self):
with self.assertRaises(NameError):
ForwardRef("doesntexist").evaluate()

def test_fwdref_invalid_syntax(self):
fr = ForwardRef("if")
with self.assertRaises(SyntaxError):
fr.evaluate()
fr = ForwardRef("1+")
with self.assertRaises(SyntaxError):
fr.evaluate()


class TestGetAnnotations(unittest.TestCase):
def test_builtin_type(self):
Expand Down

0 comments on commit cebb78e

Please sign in to comment.