Skip to content

assert_type fails to fail for NoReturn #16319

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

Open
ghost opened this issue Oct 23, 2023 · 2 comments · May be fixed by #18707
Open

assert_type fails to fail for NoReturn #16319

ghost opened this issue Oct 23, 2023 · 2 comments · May be fixed by #18707
Labels
bug mypy got something wrong

Comments

@ghost
Copy link

ghost commented Oct 23, 2023

Bug Report

In a case that mypy deems impossible (i.e. type narrowed to NoReturn), it's currently impossible to cause assert_type to fail.

Similarly, reveal_type has no effect in the case of NoReturn.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=10400960688da44ab22b58df6c297f50

Expected Behavior

Three assert_type failures.

Actual Behavior

Only one assert_type failure.

@ghost ghost added the bug mypy got something wrong label Oct 23, 2023
@sobolevn
Copy link
Member

This happens because mypy does not execute code for Literal[False] branches in if.

Check this:

"""All three of these assert_type should fail, but only one does, currently."""
import typing_extensions as t

def f(x: int | bool):
    if isinstance(x, str):
        reveal_type(x)  # not executed

@sobolevn
Copy link
Member

This does not look like a bug to me.

@A5rocks A5rocks linked a pull request Mar 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant