Skip to content

Commit 012d52c

Browse files
authored
Don't consider None vs IntEnum comparison ambiguous (#17877)
We can assume that None doesn't compare equal to an enum.
1 parent 9330193 commit 012d52c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8645,6 +8645,8 @@ def _ambiguous_enum_variants(types: list[Type]) -> set[str]:
86458645
result.add("<other>")
86468646
elif isinstance(t, LiteralType):
86478647
result.update(_ambiguous_enum_variants([t.fallback]))
8648+
elif isinstance(t, NoneType):
8649+
pass
86488650
else:
86498651
result.add("<other>")
86508652
return result

test-data/unit/check-narrowing.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,20 @@ def f6(x: IE | Any) -> None:
22432243
reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]"
22442244
else:
22452245
reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]"
2246+
2247+
def f7(x: IE | None) -> None:
2248+
if x == IE.X:
2249+
reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]"
2250+
else:
2251+
reveal_type(x) # N: Revealed type is "Union[Literal[__main__.IE.Y], None]"
2252+
2253+
def f8(x: IE | None) -> None:
2254+
if x is None:
2255+
reveal_type(x) # N: Revealed type is "None"
2256+
elif x == IE.X:
2257+
reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]"
2258+
else:
2259+
reveal_type(x) # N: Revealed type is "Literal[__main__.IE.Y]"
22462260
[builtins fixtures/primitives.pyi]
22472261

22482262
[case testNarrowingWithStrEnum]

0 commit comments

Comments
 (0)