[Python-checkins] [3.10] bpo-44468: Never skip base classes in `typing.get_type_hints()`, even with invalid `.__module__`. (GH-26862) (GH-26920)

miss-islington webhook-mailer at python.org
Sat Jun 26 19:52:35 EDT 2021


https://github.com/python/cpython/commit/3df23b5199a4bb237a595cadca6c49d34ab2a56c
commit: 3df23b5199a4bb237a595cadca6c49d34ab2a56c
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-06-26T16:52:28-07:00
summary:

[3.10] bpo-44468: Never skip base classes in `typing.get_type_hints()`, even with invalid `.__module__`. (GH-26862) (GH-26920)



(cherry picked from commit 7569c0fe91dfcf562dee8c29798ecda74d738aa8)


Co-authored-by: will-ca <willchencontact at gmail.com>

Automerge-Triggered-By: GH:gvanrossum

files:
A Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.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 79c5c3a910407..07f809ca301e8 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2276,13 +2276,6 @@ def test_no_isinstance(self):
         with self.assertRaises(TypeError):
             issubclass(int, ClassVar)
 
-    def test_bad_module(self):
-        # bpo-41515
-        class BadModule:
-            pass
-        BadModule.__module__ = 'bad' # Something not in sys.modules
-        self.assertEqual(get_type_hints(BadModule), {})
-
 class FinalTests(BaseTestCase):
 
     def test_basics(self):
@@ -3032,6 +3025,24 @@ class Foo:
         # This previously raised an error under PEP 563.
         self.assertEqual(get_type_hints(Foo), {'x': str})
 
+    def test_get_type_hints_bad_module(self):
+        # bpo-41515
+        class BadModule:
+            pass
+        BadModule.__module__ = 'bad' # Something not in sys.modules
+        self.assertNotIn('bad', sys.modules)
+        self.assertEqual(get_type_hints(BadModule), {})
+
+    def test_get_type_hints_annotated_bad_module(self):
+        # See https://bugs.python.org/issue44468
+        class BadBase:
+            foo: tuple
+        class BadType(BadBase):
+            bar: list
+        BadType.__module__ = BadBase.__module__ = 'bad'
+        self.assertNotIn('bad', sys.modules)
+        self.assertEqual(get_type_hints(BadType), {'foo': tuple, 'bar': list})
+
 
 class GetUtilitiesTestCase(TestCase):
     def test_get_origin(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index 639feeef99e75..9540021e8c0a6 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1700,10 +1700,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
         hints = {}
         for base in reversed(obj.__mro__):
             if globalns is None:
-                try:
-                    base_globals = sys.modules[base.__module__].__dict__
-                except KeyError:
-                    continue
+                base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
             else:
                 base_globals = globalns
             ann = base.__dict__.get('__annotations__', {})
diff --git a/Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst b/Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst
new file mode 100644
index 0000000000000..78251c7d55068
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst
@@ -0,0 +1,2 @@
+:func:`typing.get_type_hints` now finds annotations in classes and base classes
+with unexpected ``__module__``. Previously, it skipped those MRO elements.



More information about the Python-checkins mailing list