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.

Title: NoReturn not allowed by get_type_hints when future import annotations is used
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: itoijala, levkivskyi, ngwood111
Priority: normal Keywords: patch

Created on 2018-10-07 11:46 by itoijala, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9750 merged ngwood111, 2018-10-07 18:21
PR 10772 merged itoijala, 2018-11-28 16:59
Messages (5)
msg327274 - (view) Author: Ismo Toijala (itoijala) * Date: 2018-10-07 11:46
The following example should work but does not.
Note that it does work without the future import.

from __future__ import annotations

import typing

def f() -> typing.NoReturn:


Traceback (most recent call last):
  File "", line 8, in <module>
  File "/usr/lib/python3.7/", line 1001, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/usr/lib/python3.7/", line 260, in _eval_type
    return t._evaluate(globalns, localns)
  File "/usr/lib/python3.7/", line 466, in _evaluate
  File "/usr/lib/python3.7/", line 135, in _type_check
    raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.NoReturn is not valid as type argument
msg327296 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-07 17:56
Thanks for the report. I am adding Ivan who might have a better explanation since this is related to typing and type(NoReturn) has <class 'typing._SpecialForm'> with __future__ import and <class 'type'> without __future__. Ivan, feel free to remove yourself if this irrelevant.
msg327302 - (view) Author: noah (ngwood111) * Date: 2018-10-07 18:28
Is this a feature request? Because it doesn't look like a bug to me. Where NoReturn is defined it says it's supposed to fail in static type checkers.

Anyway, I'm not entirely sure on the whole process of contributing but here goes:

The code ultimately fails at _type_check.
According to _type_check, "special forms like Union are not valid, while Union[int, str] is OK, etc."

NoReturn isn't subscriptable.

So basically the code is getting to this point and executing

    _type_check(NoReturn, msg)

which fails on any special form.

If you try to force NoReturn to have additional parameters it will fail at __getitem__ because NoReturn is not subscriptable.

Any is also not subscriptable, but it's specifically handled in the _type_check() function ala:

    if (isinstance(arg, _SpecialForm) and arg is not Any or ...

if you wanted to add NoReturn you could do something like

    if (isinstance(arg, _SpecialForm) and arg not in [Any, NoReturn] or ...

Tested it and it works fine on 3.7 and 3.8 for me!

I've submitted a pull request with my proposed fix
msg327368 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-10-08 18:50
New changeset 5eea0ad50c32d38909ff4e29309e2cc3c6ccb2c0 by Ivan Levkivskyi (Noah Wood) in branch 'master':
bpo-34921: Allow escaped NoReturn in get_type_hints (GH-9750)
msg330617 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-11-28 17:22
New changeset f71a5922916abd6cc7bf7d99ed4715b6e96e5981 by Ivan Levkivskyi (Ismo Toijala) in branch '3.7':
bpo-34921: Allow escaped NoReturn in get_type_hints (GH-9750) (GH-10772)
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79102
2018-11-28 17:22:13levkivskyisetmessages: + msg330617
2018-11-28 16:59:35itoijalasetpull_requests: + pull_request10016
2018-10-08 19:04:38levkivskyisetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-10-08 18:50:21levkivskyisetnosy: + levkivskyi
messages: + msg327368
2018-10-07 18:28:54ngwood111setnosy: + ngwood111, - levkivskyi, xtreak

messages: + msg327302
versions: + Python 3.8
2018-10-07 18:21:57ngwood111setkeywords: + patch
stage: patch review
pull_requests: + pull_request9136
2018-10-07 17:56:51xtreaksetnosy: + xtreak, levkivskyi
messages: + msg327296
2018-10-07 11:46:05itoijalacreate