This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: typing.get_type_hints generates KeyError
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: WCA, eric.fahlgren, gvanrossum, levkivskyi, python-dev, vstinner, xtreak
Priority: normal Keywords: patch

Created on 2020-08-10 13:43 by eric.fahlgren, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21816 closed python-dev, 2020-08-10 19:40
PR 25352 merged xtreak, 2021-04-12 06:02
PR 25379 merged xtreak, 2021-04-13 03:15
PR 26862 WCA, 2021-06-23 17:25
Messages (9)
msg375111 - (view) Author: Eric Fahlgren (eric.fahlgren) * Date: 2020-08-10 13:43
Windows 10 Pro 64
Python 3.8.3 64
wxPython 4.1.0

It appears that there are synthetic classes in the mro, which don't appear in the type's namespace, raising KeyError when encountered.  From reading the function's doc and source, it looks like it should handle this internally and return a valid dict (possibly empty), and not raise KeyError.

>>> import typing, wx
>>> typing.get_type_hints(wx.Window)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python38\lib\typing.py", line 1223, in get_type_hints
    base_globals = sys.modules[base.__module__].__dict__

>>> wx.Window.mro()
[<class 'wx._core.Window'>, <class 'wx._core.WindowBase'>, <class 'wx._core.EvtHandler'>, <class 'wx._core.Object'>, <class 'wx._core.Trackable'>, <class 'sip.wrapper'>, <class 'sip.simplewrapper'>, <class 'object'>]

KeyError: 'sip'
msg375127 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-08-10 15:56
Hm, the fix would seem simple enough. Can you submit a PR? It would be nice if it had a test as well.
msg390819 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-12 05:39
I guess more libraries will experience this with `from __future__ import annotations` becoming default in Python 3.10 and they switch to get_type_hints.

https://github.com/sphinx-doc/sphinx/issues/8084
https://github.com/facebook/TestSlide/issues/296
msg390882 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-04-12 18:17
New changeset a9cf69df2e031d6157f01289f66ca9cf723ceb5c by Karthikeyan Singaravelan in branch 'master':
bpo-41515: Fix KeyError raised in get_type_hints (GH-25352)
https://github.com/python/cpython/commit/a9cf69df2e031d6157f01289f66ca9cf723ceb5c
msg390891 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-12 19:30
This change introduced a regression:
https://buildbot.python.org/all/#/builders/96/builds/1012

$ ./python -m test test___all__
0:00:00 load avg: 0.28 Run tests sequentially
0:00:00 load avg: 0.28 [1/1] test___all__
test test___all__ failed -- Traceback (most recent call last):
  File "/home/vstinner/python/master/Lib/test/test___all__.py", line 104, in test_all
    self.check_all(modname)
  File "/home/vstinner/python/master/Lib/test/test___all__.py", line 25, in check_all
    exec("import %s" % modname, names)
  File "/home/vstinner/python/master/Lib/contextlib.py", line 140, in __exit__
    next(self.gen)
  File "/home/vstinner/python/master/Lib/test/support/warnings_helper.py", line 177, in _filterwarnings
    raise AssertionError("unhandled warning %s" % reraise[0])
AssertionError: unhandled warning {message : SyntaxWarning('assertion is always true, perhaps remove parentheses?'), category : 'SyntaxWarning', filename : '/home/vstinner/python/master/Lib/test/test_typing.py', lineno : 2275, line : None}

test___all__ failed

== Tests result: FAILURE ==

1 test failed:
    test___all__

Total duration: 2.7 sec
Tests result: FAILURE


It comes from the following line of test_bad_module() in Lib/test/test_typing:

    assert(get_type_hints(BadModule), {})

assert should be used as "assert test" or "assert test, expr". assert (expr, expr) tests if the tuple (2 items) is true, and it's always true.
msg390892 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-04-12 19:33
Thanks -- odd this didn't fail in CI. We'll get it fixed.
msg390893 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-12 19:42
> Thanks -- odd this didn't fail in CI.

The CI runs tests with "make buildbottest" which uses the -w option:

  -w, --verbose2        re-run failed tests in verbose mode

When test___all__ is re-run in verbose mode, it doesn't fail:

"== Tests result: FAILURE then SUCCESS =="
msg390919 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-13 03:17
Thanks Victor. I have created https://github.com/python/cpython/pull/25379 that uses self.assertEqual instead of assert that produces syntax warning.
msg390979 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-04-13 17:15
I see that all PRs are merged, and the CI issue is fixed as well. I close the issue.

commit eb77133564d74eb09ed63872a69b9827d4841b49
Author: Karthikeyan Singaravelan <tir.karthi@gmail.com>
Date:   Tue Apr 13 19:24:23 2021 +0530

    bpo41515: Fix assert in test which throws SyntaxWarning. (#25379)
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85687
2021-06-23 17:25:50WCAsetnosy: + WCA

pull_requests: + pull_request25458
2021-04-13 17:15:45vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg390979

stage: patch review -> resolved
2021-04-13 03:17:59xtreaksetmessages: + msg390919
2021-04-13 03:15:28xtreaksetpull_requests: + pull_request24111
2021-04-12 19:42:22vstinnersetmessages: + msg390893
2021-04-12 19:33:40gvanrossumsetmessages: + msg390892
2021-04-12 19:30:03vstinnersetnosy: + vstinner
messages: + msg390891
2021-04-12 18:17:32gvanrossumsetmessages: + msg390882
2021-04-12 06:02:01xtreaksetpull_requests: + pull_request24087
2021-04-12 05:39:28xtreaksetnosy: + xtreak
messages: + msg390819
2020-08-10 19:40:07python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request20948
stage: patch review
2020-08-10 15:56:16gvanrossumsetmessages: + msg375127
2020-08-10 14:29:27xtreaksetnosy: + gvanrossum, levkivskyi
2020-08-10 13:43:58eric.fahlgrencreate