bpo-41515: Fix KeyError raised in get_type_hints (GH-25352)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: efahl <36704995+efahl@users.noreply.github.com>
This commit is contained in:
Karthikeyan Singaravelan 2021-04-12 23:47:25 +05:30 committed by GitHub
parent 852150ddfe
commit a9cf69df2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View file

@ -2267,6 +2267,12 @@ class ClassVarTests(BaseTestCase):
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):

View file

@ -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:
try:
base_globals = sys.modules[base.__module__].__dict__
except KeyError:
continue
else:
base_globals = globalns
ann = base.__dict__.get('__annotations__', {})

View file

@ -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``.