Skip to content

Commit 1e2c3f8

Browse files
committed
Adapt code into the latest changes
1 parent 6bd5fee commit 1e2c3f8

File tree

7 files changed

+16
-22
lines changed

7 files changed

+16
-22
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,7 @@ any valid Python expression. The presence of annotations does not change the
613613
semantics of a function. The annotation values are available as values of
614614
a dictionary keyed by the parameters' names in the :attr:`__annotations__`
615615
attribute of the function object. Used annnotations are preserved as strings at
616-
runtime which enables postponed evaluation (annotations may be evaluated in a
617-
different order than they appear in the source code).
616+
runtime which enables postponed evaluation.
618617

619618
.. index:: pair: lambda; expression
620619

Lib/test/test_annotations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def test_annotations(self):
198198
eq('(((a, b)))', '(a, b)')
199199
eq("(x := 10)")
200200
eq("f'{(x := 10):=10}'")
201+
eq("1 + 2")
201202
eq("1 + 2 + 3")
202203

203204
def test_fstring_debug_annotations(self):

Lib/test/test_functools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def check_wrapper(self, wrapper, wrapped,
618618

619619

620620
def _default_update(self):
621-
def f(a:'This is a new annotation'):
621+
def f(a: int):
622622
"""This is a test"""
623623
pass
624624
f.attr = 'This is also a test'
@@ -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'], repr('This is a new annotation'))
638+
self.assertEqual(wrapper.__annotations__['a'], 'int')
639639
self.assertNotIn('b', wrapper.__annotations__)
640640

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

Lib/test/test_grammar.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,6 @@ def f(*x: str): pass
636636
self.assertEqual(f.__annotations__, {'x': 'str'})
637637
def f(**x: float): pass
638638
self.assertEqual(f.__annotations__, {'x': 'float'})
639-
def f(x, y: 1+2): pass
640-
self.assertEqual(f.__annotations__, {'y': '1 + 2'})
641-
def f(x, y: 1+2, /): pass
642-
self.assertEqual(f.__annotations__, {'y': '1 + 2'})
643639
def f(a, b: 1, c: 2, d): pass
644640
self.assertEqual(f.__annotations__, {'b': '1', 'c': '2'})
645641
def f(a, b: 1, /, c: 2, d): pass

Lib/test/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ def test_or_type_operator_with_forward(self):
671671
ForwardBefore = 'Forward' | T
672672
def forward_after(x: ForwardAfter[int]) -> None: ...
673673
def forward_before(x: ForwardBefore[int]) -> None: ...
674-
assert typing.get_args(typing.get_type_hints(forward_after)['x']) == (int, Forward)
675-
assert typing.get_args(typing.get_type_hints(forward_before)['x']) == (int, Forward)
674+
assert typing.get_args(typing.get_type_hints(forward_after, localns=locals())['x']) == (int, Forward)
675+
assert typing.get_args(typing.get_type_hints(forward_before, localns=locals())['x']) == (int, Forward)
676676

677677
def test_or_type_operator_with_Protocol(self):
678678
class Proto(typing.Protocol):

Lib/test/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,7 @@ def test_get_type_hints_classes(self):
28492849
self.assertEqual(gth(HasForeignBaseClass),
28502850
{'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
28512851
'some_b': mod_generics_cache.B})
2852-
self.assertEqual(gth(XRepr.__new__),
2852+
self.assertEqual(gth(XRepr),
28532853
{'x': int, 'y': int})
28542854
self.assertEqual(gth(mod_generics_cache.B),
28552855
{'my_inner_a1': mod_generics_cache.B.A,

Lib/typing.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
from abc import abstractmethod, ABCMeta
21+
import ast
2122
import collections
2223
import collections.abc
2324
import contextlib
@@ -469,11 +470,13 @@ class ForwardRef(_Final, _root=True):
469470
def __init__(self, arg, is_argument=True):
470471
if not isinstance(arg, str):
471472
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 "'\"":
473+
474+
# Double-stringified forward references is a result of activating
475+
# 'annotations' future by default. This way, we eliminate them on
476+
# the runtime.
477+
if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
476478
arg = arg[1:-1]
479+
477480
try:
478481
code = compile(arg, '<string>', 'eval')
479482
except SyntaxError:
@@ -1366,11 +1369,6 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
13661369
locals, respectively.
13671370
"""
13681371

1369-
def resolve(value, globalns, localns=localns):
1370-
# double resolve forward refs
1371-
value = _eval_type(value, globalns, localns)
1372-
return _eval_type(value, globalns, localns)
1373-
13741372
if getattr(obj, '__no_type_check__', None):
13751373
return {}
13761374
# Classes require a special treatment.
@@ -1387,7 +1385,7 @@ def resolve(value, globalns, localns=localns):
13871385
value = type(None)
13881386
if isinstance(value, str):
13891387
value = ForwardRef(value, is_argument=False)
1390-
value = resolve(value, base_globals)
1388+
value = _eval_type(value, base_globals, localns)
13911389
hints[name] = value
13921390
return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
13931391

@@ -1419,7 +1417,7 @@ def resolve(value, globalns, localns=localns):
14191417
value = type(None)
14201418
if isinstance(value, str):
14211419
value = ForwardRef(value)
1422-
value = resolve(value, globalns)
1420+
value = _eval_type(value, globalns, localns)
14231421
if name in defaults and defaults[name] is None:
14241422
value = Optional[value]
14251423
hints[name] = value

0 commit comments

Comments
 (0)