-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 narrowing enum values using ==
and !=
#10915
Comments
If you use the |
Why? is it an anti pattern to use |
I don't know why mypy handles |
==
and !=
If you want to give implementing this a shot take a look at #7000, in particular #7000 (comment) |
@hauntsaninja Would this be as simple as changing: Line 4476 in 421fcc2
To be |
Yeah, should be something like that. You'll want to restrict to when the comparison is against an Enum that has a last_known_value. Let me play around and see... |
|
This is what I'm playing with:
We do have test cases that explicitly test against this behaviour though :-( most notably |
Were they added when enums To make sure that they wouldn't work on |
But now that we are implementing it correctly we should change the tests. |
|
It'd be the case in
Probably fine to omit that check, since Yeah, the tests are pretty explicit about why they test what they test: mypy/test-data/unit/check-narrowing.test Line 720 in d41e34a
I'm not convinced that those tests are necessarily the behaviour we want, but seems like whoever wrote them collected some data / spent more time thinking about this specific issue than I have, so just saying I'd need to think about it too! :-) |
That looks like a scenario like: def m():
global a
a = 2
a: int = True
if a is True:
m()
reveal_type(a) # Literal[True] Sus! |
What's the policy on narrowing mutable variables? seems to me like that example with narrowing to |
Yeah, I'm inclined to agree (although literal types always make things murkier in practice). Although the test case specifically tests enums, it's possible that the real world false positives alluded to were more often narrowing to literal strs or something. I can make the PR and we'll see what other mypy maintainers have to say. |
Resolves python#10915, resolves python#9786 See the discussion in python#10915. I'm sympathetic to the difference between identity and equality here being surprising and that mypy doesn't usually make concessions to mutability when type checking. The old test cases are pretty explicit about their intentions and are worth reading. Curious to see what people (and mypy-primer) have to say about this.
Last I checked (which was all the way back in version 0.790), the behavior was even stranger than presented here. |
This makes |
I don't really use |
Good point, I don't know why I used a But I could imagine if anyone ever does use a |
Resolves #10915, resolves #9786 See the discussion in #10915. I'm sympathetic to the difference between identity and equality here being surprising and that mypy doesn't usually make concessions to mutability when type checking. The old test cases are pretty explicit about their intentions and are worth reading. Curious to see what people (and mypy-primer) have to say about this. Co-authored-by: hauntsaninja <>
https://mypy-play.net/?mypy=latest&python=3.10&flags=show-column-numbers%2Cshow-error-codes%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-expr%2Cdisallow-any-explicit%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cwarn-incomplete-stub%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=0f58b3bb7b93c7e3d917c18a7df2ed39
The text was updated successfully, but these errors were encountered: