-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Support exhausted matching on enums #767
Comments
Thanks for the suggestion. I didn't know that mypy supported this special use of NoReturn. I've added the same for Pyright. It can be used for all forms of type narrowing, not just for enums. For example:
|
This is now implemented in version 1.1.47, which I just published. |
Thanks for the quick response! I can confirm that pyright now understands the NoReturn trick. However, if I change |
Yeah, that's a separate issue. Currently, type narrowing supports "is" and "is not" operators only for None values, since that's the most common pattern. I didn't realize you could use "is" in place of "==" for other types. Is there a difference between the two? |
My general philosophy for type narrowing support is to support the most common expression types and calls and not the more esoteric ones. Each one requires hand-coded logic and adds complexity. Since no one else has requested this broader support for "is" previously, I will likely not add it unless/until it is upvoted — especially given that there's such an easy workaround. |
Thanks for that, I've added a new issue #779 with details on the differences so others can add upvote it if they want |
Describe the bug
Mypy supports exhaustive matching on Enums, so if an entry is added later and not checked, it will show an error. An example is python/mypy#6366 (comment)
To Reproduce
pyright correctly gives an error for a:
Argument of type "Literal[SomeEnum.VALUE3]" cannot be assigned to parameter "x" of type "NoReturn" in function "assert_never". "Literal[SomeEnum.VALUE3]" is incompatible with "NoReturn"Pyright (reportGeneralTypeIssues)
Change
a ==
toa is
. pyright gives an error for a:Argument of type "SomeEnum" cannot be assigned to parameter "x" of type "NoReturn" in function "assert_never". "SomeEnum" is incompatible with "NoReturn"Pyright (reportGeneralTypeIssues)
Comment out
VALUE3 = 3
. pyright gives an error for a:Argument of type "Never" cannot be assigned to parameter "x" of type "NoReturn" in function "assert_never". Type "Never" cannot be assigned to type "NoReturn"Pyright (reportGeneralTypeIssues)
Expected behavior
VS Code extension or command-line
VS Code extension v1.1.46
Additional context
Thanks for #653 , Enum support is really good now!
The text was updated successfully, but these errors were encountered: