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

reveal_type not run in "unreachable" branches #18423

Closed
hjwp opened this issue Jan 6, 2025 · 3 comments
Closed

reveal_type not run in "unreachable" branches #18423

hjwp opened this issue Jan 6, 2025 · 3 comments
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@hjwp
Copy link

hjwp commented Jan 6, 2025

Bug Report

It looks like reveal_type() doesn't work in "unreachable" branches, ie places where assert_never() passes.

To Reproduce

from typing import assert_never, reveal_type


def foo(b: bool):
    reveal_type(b)
    match b:
        case True:
            reveal_type(b)
            print("True")
        case False:
            reveal_type(b)
            print("False")
        case _:
            assert_never(b)
            reveal_type(b)  # this one does not print

(also at https://mypy-play.net/?mypy=latest&python=3.12&gist=d2646350c3ec4aa892763cdda7d32201)

Actual Behavior

❯ mypy thing.py
thing.py:5: note: Revealed type is "builtins.bool"
thing.py:8: note: Revealed type is "Literal[True]"
thing.py:11: note: Revealed type is "Literal[False]"
Success: no issues found in 1 source file

Expected behaviour

I would expect the 4th reveal_type() to print something like Revealed type is Never ?

(note that the assert_never() does "pass").

Your Environment

mypy 1.14.1 (compiled: yes)
Python 3.13.0 (also seen in 3.12)

@A5rocks
Copy link
Collaborator

A5rocks commented Jan 23, 2025

This is intentional because mypy doesn't type check unreachable code. For instance, see also #18329.

@A5rocks A5rocks added the topic-reachability Detecting unreachable code label Jan 23, 2025
@hjwp
Copy link
Author

hjwp commented Jan 23, 2025

HI @A5rocks thanks for answering! I was confused because I thought assert_never() used the type checker, so implicitly the unreachable branch must be being type checked, and now i understand, i think -- assert_never() only causes a typing failure when the branch is not unreachable.

Shall we close this issue?

@A5rocks
Copy link
Collaborator

A5rocks commented Jan 23, 2025

Tbh yeah this is tracked by that other issue.

(I'm not sure the specifics on how assert never works...)

@A5rocks A5rocks closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants