GeezerVison is showing github.com — support narrowing enum values using `==` and `!=` · Issue #10915 · python/mypy
support narrowing enum values using == and != #10915DescriptionReacted by KotlinIsland and Felipe S. S. Schneider Activityerictraut commented on Aug 3, 2021 If you use the is and is not operators rather than == and !=, mypy will perform narrowing for enum values. KotlinIsland commented on Aug 3, 2021 Why? is it an anti pattern to use ==/!= on enum values? if so should a warning be raised when they are used?. erictraut commented on Aug 3, 2021 I don't know why mypy handles is and is not but not == and != in this case. I'm guessing that the former is more common than the latter. I think both patterns are reasonable. I can't think of a reason why == and != would be an anti-pattern. FWIW, pyright supports type narrowing for both pairs of operators. Reacted by KotlinIsland and Felipe S. S. Schneider changed the title [-]support narrowing enum values[/-] [+]support narrowing enum values using `==` and `!=`[/+] on Aug 3, 2021hauntsaninja commented on Aug 3, 2021 KotlinIsland commented on Nov 11, 2021 @hauntsaninja Would this be as simple as changing:
|
coerce_only_in_literal_context = True |
To be False if the comparison is against an Enum? hauntsaninja commented on Nov 11, 2021 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... KotlinIsland commented on Nov 11, 2021 coerce_only_in_literal_context = False if all(isinstance(p := get_proper_type(t), Instance) and p.type.is_enum for t in expr_types) else True
I wrote this, but it looks pretty cringe.
hauntsaninja commented on Nov 11, 2021 This is what I'm playing with:
diff --git a/mypy/checker.py b/mypy/checker.py
index 95789831f..d8e65f73c 100644
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -4802,7 +4802,16 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
"""
should_coerce = True
if coerce_only_in_literal_context:
- should_coerce = any(is_literal_type_like(operand_types[i]) for i in chain_indices)
+
+ def should_coerce_inner(typ: Type) -> bool:
+ typ = get_proper_type(typ)
+ return is_literal_type_like(typ) or (
+ isinstance(typ, Instance)
+ and typ.type.is_enum
+ and (typ.last_known_value is not None)
+ )
+
+ should_coerce = any(should_coerce_inner(operand_types[i]) for i in chain_indices)
target: Optional[Type] = None
possible_target_indices = []
We do have test cases that explicitly test against this behaviour though :-( most notably testNarrowingEqualityFlipFlop. KotlinIsland commented on Nov 11, 2021 Were they added when enums is comparisons were added.
To make sure that they wouldn't work on == due to complications. KotlinIsland commented on Nov 11, 2021 But now that we are implementing it correctly we should change the tests. KotlinIsland commented on Nov 11, 2021 and (typ.last_known_value is not None)
When would it be an enum and None?
hauntsaninja commented on Nov 11, 2021 It'd be the case in
def f(x: Medal, y: Medal):
if x == y: ...
Probably fine to omit that check, since coerce_to_literal won't do anything if last_known_value is None (actually that's not true, it'll narrow singleton enums as well, so probably good to omit it, barring some chaining weirdness).
Yeah, the tests are pretty explicit about why they test what they test:
|
# So strictly speaking, we ought to do the same thing with 'is' comparisons |
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! :-) KotlinIsland commented on Nov 11, 2021 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! KotlinIsland commented on Nov 11, 2021 What's the policy on narrowing mutable variables? seems to me like that example with narrowing to True is the same as that test? hauntsaninja commented on Nov 11, 2021 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. added a commit that references this issue on Nov 11, 2021This issue will close once commit db11aa5 is merged into the 'master' branch.db11aa5 parsons-kyle-89 commented on Apr 1, 2022 Last I checked (which was all the way back in version 0.790), the behavior was even stranger than presented here. == did narrow narrow Enums in if-else blocks, but only if they followed an is. See #9786 Reacted by DetachHead and KotlinIsland KotlinIsland commented on Feb 9, 2023 This makes match on enums really annoying. hauntsaninja commented on Feb 9, 2023 I don't really use match. Could you paste a semi-realistic example where merging #11521 would help? KotlinIsland commented on Feb 9, 2023 Good point, I don't know why I used a match...
But I could imagine if anyone ever does use a match that this will be annoying. added a commit that references this issue on Apr 24, 2023This issue will close once commit 0061d6e is merged into the 'master' branch.0061d6e
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|