Skip to content

Commit bbf01bb

Browse files
committed
Escape double stringified annotations on typing.ForwardRef
1 parent 6209fa0 commit bbf01bb

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Lib/test/test_functools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ def test_default_update(self):
635635
self.assertEqual(wrapper.__name__, 'f')
636636
self.assertEqual(wrapper.__qualname__, f.__qualname__)
637637
self.assertEqual(wrapper.attr, 'This is also a test')
638-
self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation')
638+
self.assertEqual(wrapper.__annotations__['a'], repr('This is a new annotation'))
639639
self.assertNotIn('b', wrapper.__annotations__)
640640

641641
@unittest.skipIf(sys.flags.optimize >= 2,

Lib/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ class ForwardRef(_Final, _root=True):
469469
def __init__(self, arg, is_argument=True):
470470
if not isinstance(arg, str):
471471
raise TypeError(f"Forward reference must be a string -- got {arg!r}")
472+
# since annotations feature is now default, stringified annotations
473+
# should be escaped from quotes, or this will result with double
474+
# forward refs.
475+
if arg[0] in "'\"" and arg[-1] in "'\"":
476+
arg = arg[1:-1]
472477
try:
473478
code = compile(arg, '<string>', 'eval')
474479
except SyntaxError:

0 commit comments

Comments
 (0)