-
Notifications
You must be signed in to change notification settings - Fork 767
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
Type Union with NoReturn #2008
Comments
Thanks for reporting the issue. I've created a tracking bug in the pyright issue tracker. |
We already elide |
Thank you for the quick reply. from typing import NoReturn
def raises(exception: Exception) -> NoReturn:
'''Helper function to allow raising an exception in an expression context'''
raise exception
n = 1
n_str: str
# incorrect type ERROR (assigning str | NoReturn to var of type str):
n_str = str(n) or raises(RuntimeError(f'Must be positive: N = {n}'))
print(n_str) |
I've added support for |
fixed in 2021.11.0 |
Environment data
Expected behaviour
No type errors reported
Actual behaviour
Error resulting from
Union[A, B, ..., NoReturn]
not being considered equivalent toUnion[A, B, ...]
which I believe it should.To be clear, a [variable, function_arg, return_type] of type
A
should not accept an expression of typeNoReturn
(since this is either useless or a bug) in which case a type error is correctly reported. However, a scenario where a typeA
is expected should accept an expression of typeUnion[A, NoReturn]
.Notice that, in Python, the evaluation of any expression may result in an
Exception
being raised (ex:MemoryError
). This means that an expression of some typeA
is always implicitly of typeUnion[A, NoReturn]
.Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: