From 43288b6511dc977d7efbc93a0d9c0746c0d017fb Mon Sep 17 00:00:00 2001 From: will-ca Date: Tue, 22 Jun 2021 22:35:03 +0000 Subject: [PATCH 1/5] Better tests for get_type_hints() with invalid .__module__. --- Lib/test/test_typing.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index eff0f5bfc4b1e16..ecf6b00c13c1164 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2282,8 +2282,19 @@ def test_bad_module(self): class BadModule: pass BadModule.__module__ = 'bad' # Something not in sys.modules + self.assertNotIn('bad', sys.modules) self.assertEqual(get_type_hints(BadModule), {}) + def test_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 FinalTests(BaseTestCase): def test_basics(self): From 26ddcf360363bfb71185df854bd7b6b25e61b369 Mon Sep 17 00:00:00 2001 From: will-ca Date: Tue, 22 Jun 2021 22:37:52 +0000 Subject: [PATCH 2/5] Move get_type_hints() test to right class. --- Lib/test/test_typing.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index ecf6b00c13c1164..896781a8bafee53 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2277,24 +2277,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.assertNotIn('bad', sys.modules) - self.assertEqual(get_type_hints(BadModule), {}) - - def test_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 FinalTests(BaseTestCase): def test_basics(self): @@ -3044,6 +3026,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): From 6fea8f8a905ca8df5bd1898dffd265a164e4c6a8 Mon Sep 17 00:00:00 2001 From: will-ca Date: Tue, 22 Jun 2021 22:44:52 +0000 Subject: [PATCH 3/5] bpo-44468 --- Lib/typing.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 00a0df591cbfccd..2287f0521a364f1 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1701,10 +1701,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__', {}) From 4c8433b33f2c4165811318396fca539302972a73 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 23 Jun 2021 19:02:01 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst 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 000000000000000..b13944b51b162b9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst @@ -0,0 +1 @@ +:func:`typing.get_type_hints` now finds annotations in classes and base classes with unexpected ``__module__``. Previously, it skip those MRO elements. \ No newline at end of file From 090bf13b927e0eada702110806c0276f265e9584 Mon Sep 17 00:00:00 2001 From: will-ca Date: Wed, 23 Jun 2021 19:03:16 +0000 Subject: [PATCH 5/5] Update 2021-06-23-19-02-00.bpo-44468.-klV5-.rst --- .../next/Library/2021-06-23-19-02-00.bpo-44468.-klV5-.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index b13944b51b162b9..78251c7d55068dd 100644 --- 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 @@ -1 +1,2 @@ -:func:`typing.get_type_hints` now finds annotations in classes and base classes with unexpected ``__module__``. Previously, it skip those MRO elements. \ No newline at end of file +:func:`typing.get_type_hints` now finds annotations in classes and base classes +with unexpected ``__module__``. Previously, it skipped those MRO elements.