Skip to content

TypeGuard is incompatible with exhaustiveness checking #15305

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

Closed
antonagestam opened this issue May 25, 2023 · 3 comments
Closed

TypeGuard is incompatible with exhaustiveness checking #15305

antonagestam opened this issue May 25, 2023 · 3 comments
Labels
bug mypy got something wrong topic-typeguard-typeis TypeGuard / TypeIs / PEP 647 / PEP 742

Comments

@antonagestam
Copy link
Contributor

antonagestam commented May 25, 2023

Bug Report

Narrowing with a TypeGuard function doesn't work in conjunction with exhaustiveness checking using assert_never().

To Reproduce

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

Expected Behavior

In the example, I expected the TypeGuard function to work no differently from the inlined type checks.

Actual Behavior

When using TypeGuard, the variable seems to be properly narrowed (revealed_type() within the branch is identical), but it seems somehow the result isn't recorded identically as the inlined checks. In the function using TypeGuard, the exhaustiveness check is not passing.

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11
@antonagestam antonagestam added the bug mypy got something wrong label May 25, 2023
@erictraut
Copy link

Mypy is correct here. TypeGuard does not perform narrowing in the negative (else) case. For the reasoning behind this, you can refer to PEP 647. There has been some discussion of adding a variant called StrictTypeGuard that is more limited than TypeGuard but allows negative type narrowing. This would require a new PEP, and it hasn't made it beyond the discussion phase yet.

@AlexWaygood AlexWaygood added the topic-typeguard-typeis TypeGuard / TypeIs / PEP 647 / PEP 742 label May 25, 2023
@AlexWaygood
Copy link
Member

What Eric said

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 25, 2023
@antonagestam
Copy link
Contributor Author

Ah my bad, thanks for explaining! 🙏

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-typeguard-typeis TypeGuard / TypeIs / PEP 647 / PEP 742
Projects
None yet
Development

No branches or pull requests

3 participants