diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f9a067bc0dfd466e8f0c26bb3c529329e0ca49bd_TGliL3Rlc3QvdGVzdF90eXBpbmcucHk=..3887d297d1e41cd2f6f63dd2d16cdb34a0ea82af_TGliL3Rlc3QvdGVzdF90eXBpbmcucHk= 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2267,6 +2267,12 @@ 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 + assert(get_type_hints(BadModule), {}) class FinalTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index f9a067bc0dfd466e8f0c26bb3c529329e0ca49bd_TGliL3R5cGluZy5weQ==..3887d297d1e41cd2f6f63dd2d16cdb34a0ea82af_TGliL3R5cGluZy5weQ== 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1628,7 +1628,10 @@ hints = {} for base in reversed(obj.__mro__): if globalns is None: - base_globals = sys.modules[base.__module__].__dict__ + try: + base_globals = sys.modules[base.__module__].__dict__ + except KeyError: + continue else: base_globals = globalns ann = base.__dict__.get('__annotations__', {}) diff --git a/Misc/NEWS.d/next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst b/Misc/NEWS.d/next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst new file mode 100644 index 0000000000000000000000000000000000000000..3887d297d1e41cd2f6f63dd2d16cdb34a0ea82af_TWlzYy9ORVdTLmQvbmV4dC9MaWJyYXJ5LzIwMjEtMDQtMTItMDYtMDEtMTAuYnBvLTQxNTE1LllhVlJlYi5yc3Q= --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst @@ -0,0 +1,2 @@ +Fix :exc:`KeyError` raised in :func:`typing.get_type_hints` due to +synthetic modules that don't appear in ``sys.modules``.