8000 bpo-38605: Make postponed evaluation of annotations default by isidentical · Pull Request #20434 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-38605: Make postponed evaluation of annotations default #20434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Escape double stringified annotations on typing.ForwardRef
  • Loading branch information
isidentical committed Oct 1, 2020
commit bbf01bba8d6992cff42d20a4de273b4acbeebb7e
2 changes: 1 addition & 1 deletion Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_default_update(self):
self.assertEqual(wrapper.__name__, 'f')
self.assertEqual(wrapper.__qualname__, f.__qualname__)
self.assertEqual(wrapper.attr, 'This is also a test')
self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation')
self.assertEqual(wrapper.__annotations__['a'], repr('This is a new annotation'))
self.assertNotIn('b', wrapper.__annotations__)

@unittest.skipIf(sys.flags.optimize >= 2,
Expand Down
5 changes: 5 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ class ForwardRef(_Final, _root=True):
def __init__(self, arg, is_argument=True):
if not isinstance(arg, str):
raise TypeError(f"Forward reference must be a string -- got {arg!r}")
# since annotations feature is now default, stringified annotations
# should be escaped from quotes, or this will result with double
# forward refs.
if arg[0] in "'\"" and arg[-1] in "'\"":
arg = arg[1:-1]
try:
code = compile(arg, '<string>', 'eval')
except SyntaxError:
Expand Down
0