Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing.get_type_hints generates KeyError #85687

Closed
efahl mannequin opened this issue Aug 10, 2020 · 9 comments
Closed

typing.get_type_hints generates KeyError #85687

efahl mannequin opened this issue Aug 10, 2020 · 9 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir

Comments

@efahl
Copy link
Mannequin

efahl mannequin commented Aug 10, 2020

BPO 41515
Nosy @gvanrossum, @vstinner, @ilevkivskyi, @efahl, @tirkarthi, @will-ca
PRs
  • bpo-41515: prevent get_type_hints from raising KeyError #21816
  • bpo-41515: Fix KeyError raised in get_type_hints #25352
  • bpo-41515: Fix assert in test which throws SyntaxWarning. #25379
  • bpo-44468: Never skip base classes in typing.get_type_hints(), even with invalid .__module__. #26862
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-04-13.17:15:45.293>
    created_at = <Date 2020-08-10.13:43:58.712>
    labels = ['3.8', 'library']
    title = 'typing.get_type_hints generates KeyError'
    updated_at = <Date 2021-06-23.17:25:50.090>
    user = 'https://github.com/efahl'

    bugs.python.org fields:

    activity = <Date 2021-06-23.17:25:50.090>
    actor = 'WCA'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-13.17:15:45.293>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2020-08-10.13:43:58.712>
    creator = 'eric.fahlgren'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41515
    keywords = ['patch']
    message_count = 9.0
    messages = ['375111', '375127', '390819', '390882', '390891', '390892', '390893', '390919', '390979']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'vstinner', 'python-dev', 'levkivskyi', 'eric.fahlgren', 'xtreak', 'WCA']
    pr_nums = ['21816', '25352', '25379', '26862']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue41515'
    versions = ['Python 3.8']

    @efahl
    Copy link
    Mannequin Author

    efahl mannequin commented Aug 10, 2020

    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'

    @efahl efahl mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir labels Aug 10, 2020
    @gvanrossum
    Copy link
    Member

    Hm, the fix would seem simple enough. Can you submit a PR? It would be nice if it had a test as well.

    @tirkarthi
    Copy link
    Member

    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.

    sphinx-doc/sphinx#8084
    facebook/TestSlide#296

    @gvanrossum
    Copy link
    Member

    New changeset a9cf69d by Karthikeyan Singaravelan in branch 'master':
    bpo-41515: Fix KeyError raised in get_type_hints (GH-25352)
    a9cf69d

    @vstinner
    Copy link
    Member

    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.

    @gvanrossum
    Copy link
    Member

    Thanks -- odd this didn't fail in CI. We'll get it fixed.

    @vstinner
    Copy link
    Member

    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 =="

    @tirkarthi
    Copy link
    Member

    Thanks Victor. I have created #25379 that uses self.assertEqual instead of assert that produces syntax warning.

    @vstinner
    Copy link
    Member

    I see that all PRs are merged, and the CI issue is fixed as well. I close the issue.

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

    bpo-41515: Fix assert in test which throws SyntaxWarning. (bpo-25379)
    

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants