From be5daa9885a521959d4dc24caf44b5a8a0c8f1d0 Mon Sep 17 00:00:00 2001 From: efahl <36704995+efahl@users.noreply.github.com> Date: Mon, 10 Aug 2020 12:26:27 -0700 Subject: [PATCH 1/2] Update typing.py --- Lib/typing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/typing.py b/Lib/typing.py index 5da032bbee8f1e..98eed6be102830 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1342,7 +1342,10 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False): 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__', {}) From ce46147310e47ca58b7f42a6737561307c20593b Mon Sep 17 00:00:00 2001 From: efahl <36704995+efahl@users.noreply.github.com> Date: Mon, 10 Aug 2020 12:34:39 -0700 Subject: [PATCH 2/2] bpo-41515: fix KeyError raised in get_type_hints typing.get_type_hints raises KeyError on synthetic modules that don't appear in sys.modules. --- Lib/test/test_typing.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 7f96aff7104551..34fbf0571b69e0 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2253,6 +2253,12 @@ 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 + assert(get_type_hints(BadModule), {}) class FinalTests(BaseTestCase):