From 79801e9cf1e645e0df6673277cad29362cd1fd5f Mon Sep 17 00:00:00 2001 From: efahl <36704995+efahl@users.noreply.github.com> Date: Mon, 12 Apr 2021 05:54:12 +0000 Subject: [PATCH 1/3] 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 ++++++ Lib/typing.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 82c517a4e600248..1ab3bd248e25cc8 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2267,6 +2267,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): diff --git a/Lib/typing.py b/Lib/typing.py index a24c01f0e3b9e36..d9a76d2bb465258 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1628,7 +1628,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 abca784740d336fbdaa802027d1216f7badc3d99 Mon Sep 17 00:00:00 2001 From: efahl <36704995+efahl@users.noreply.github.com> Date: Mon, 12 Apr 2021 06:01:25 +0000 Subject: [PATCH 2/3] Add news entry. --- .../next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst 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 000000000000000..b695f14909f5cee --- /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``. From 3623477637cdaef000555d0067e5d78ab857acf2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 12 Apr 2021 10:18:13 -0700 Subject: [PATCH 3/3] Fix markup in news item Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- .../next/Library/2021-04-12-06-01-10.bpo-41515.YaVReb.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index b695f14909f5cee..aef5c1791dfeac4 100644 --- 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 @@ -1,2 +1,2 @@ -Fix :exc:`KeyError` raised in :func:`typing.get_type_hints()` due to +Fix :exc:`KeyError` raised in :func:`typing.get_type_hints` due to synthetic modules that don't appear in ``sys.modules``.