-
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
Type narrowing with identity check #9758
Comments
The The code in your example happens to work because the Python runtime interns certain small The main purpose of a type checker is to help you write robust, maintainable code. This coding pattern does the opposite, so it's unlikely that we'd ever support the requested feature. |
On second thought, if you change your code to I also initially missed the fact that mypy implements this type narrowing form. Reopening for reconsideration. |
…pattern. Previously, narrowing was performed only when `L` was an enum or bool literal. Narrowing is also now applied for other literal types but only in the positive (`if`) direction. This addresses #9758.
Is your feature request related to a problem? Please describe.
According to the pyright documentation https://microsoft.github.io/pyright/#/type-concepts-advanced?id=type-guards, narrowing with identity check using the
is
operator is supported for booleans and enumerations. It would be nice to also be able to narrow on identity checks with other literals.Describe the solution you’d like
Currently pyright thinks that
x
could beNone
in theif
branch:I expect the type in the
if
branch to be narrowed to"Literal[1]"
. It should be safe to do this kind of narrowing here.The text was updated successfully, but these errors were encountered: