[Python-checkins] bpo-42904: Fix get_type_hints for class local namespaces (GH-24201)
gvanrossum
webhook-mailer at python.org
Mon Apr 12 13:23:21 EDT 2021
https://github.com/python/cpython/commit/852150ddfe68bc2696fc880175aeb855a0c16ae6
commit: 852150ddfe68bc2696fc880175aeb855a0c16ae6
branch: master
author: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2021-04-12T10:23:12-07:00
summary:
bpo-42904: Fix get_type_hints for class local namespaces (GH-24201)
files:
A Misc/NEWS.d/next/Library/2021-01-12-23-17-02.bpo-42904.-4qkTD.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 82c517a4e6002..062163ccf4b65 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2874,7 +2874,7 @@ def test_get_type_hints_classes(self):
{'x': int, 'y': int})
self.assertEqual(gth(mod_generics_cache.B),
{'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})
def test_respect_no_type_check(self):
@@ -3010,6 +3010,13 @@ def __iand__(self, other: Const["MySet[T]"]) -> "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):
def test_get_origin(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index a24c01f0e3b9e..583438e4740ab 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1632,12 +1632,13 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
else:
base_globals = globalns
ann = base.__dict__.get('__annotations__', {})
+ base_locals = dict(vars(base)) if localns is None else localns
for name, value in ann.items():
if value is None:
value = type(None)
if isinstance(value, str):
value = ForwardRef(value, is_argument=False)
- value = _eval_type(value, base_globals, localns)
+ value = _eval_type(value, base_globals, base_locals)
hints[name] = value
return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
diff --git a/Misc/NEWS.d/next/Library/2021-01-12-23-17-02.bpo-42904.-4qkTD.rst b/Misc/NEWS.d/next/Library/2021-01-12-23-17-02.bpo-42904.-4qkTD.rst
new file mode 100644
index 0000000000000..ef4b4e56fbebc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-01-12-23-17-02.bpo-42904.-4qkTD.rst
@@ -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.
More information about the Python-checkins
mailing list