bpo-42904: Fix get_type_hints for class local namespaces (GH-24201)

This commit is contained in:
Ken Jin 2021-04-13 01:23:12 +08:00 committed by GitHub
parent 37a5e22023
commit 852150ddfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View file

@ -2874,7 +2874,7 @@ class GetTypeHintTests(BaseTestCase):
{'x': int, 'y': int}) {'x': int, 'y': int})
self.assertEqual(gth(mod_generics_cache.B), self.assertEqual(gth(mod_generics_cache.B),
{'my_inner_a1': mod_generics_cache.B.A, {'my_inner_a1': mod_generics_cache.B.A,
'my_inner_a2': mod_generics_cache.A, 'my_inner_a2': mod_generics_cache.B.A,
'my_outer_a': mod_generics_cache.A}) 'my_outer_a': mod_generics_cache.A})
def test_respect_no_type_check(self): def test_respect_no_type_check(self):
@ -3010,6 +3010,13 @@ class GetTypeHintTests(BaseTestCase):
{'other': MySet[T], 'return': MySet[T]} {'other': MySet[T], 'return': MySet[T]}
) )
def test_get_type_hints_classes(self):
class Foo:
y = str
x: y
# This previously raised an error under PEP 563.
self.assertEqual(get_type_hints(Foo), {'x': str})
class GetUtilitiesTestCase(TestCase): class GetUtilitiesTestCase(TestCase):
def test_get_origin(self): def test_get_origin(self):

View file

@ -1632,12 +1632,13 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
else: else:
base_globals = globalns base_globals = globalns
ann = base.__dict__.get('__annotations__', {}) ann = base.__dict__.get('__annotations__', {})
base_locals = dict(vars(base)) if localns is None else localns
for name, value in ann.items(): for name, value in ann.items():
if value is None: if value is None:
value = type(None) value = type(None)
if isinstance(value, str): if isinstance(value, str):
value = ForwardRef(value, is_argument=False) value = ForwardRef(value, is_argument=False)
value = _eval_type(value, base_globals, localns) value = _eval_type(value, base_globals, base_locals)
hints[name] = value hints[name] = value
return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()} return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}

View file

@ -0,0 +1,2 @@
:func:`typing.get_type_hints` now checks the local namespace of a class when
evaluating :pep:`563` annotations inside said class.