From c206df6820be344be8ec2dfcba02272ff1486d09 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 4 Oct 2024 13:37:34 +0100 Subject: [PATCH 1/2] Don't consider None vs IntEnum comparison ambiguous We can assume that None doesn't compare equal to an enum. --- mypy/checker.py | 2 ++ test-data/unit/check-narrowing.test | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index 0d8ae3da1c9f..8d77bb02eeb2 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -8645,6 +8645,8 @@ def _ambiguous_enum_variants(types: list[Type]) -> set[str]: result.add("") elif isinstance(t, LiteralType): result.update(_ambiguous_enum_variants([t.fallback])) + elif isinstance(t, NoneType): + pass else: result.add("") return result diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 11bbf0a95084..b69cc11d2816 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -2243,6 +2243,12 @@ def f6(x: IE | Any) -> None: reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]" else: reveal_type(x) # N: Revealed type is "Union[__main__.IE, Any]" + +def f7(x: IE | None) -> None: + if x == IE.X: + reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]" + else: + reveal_type(x) # N: Revealed type is "Union[Literal[__main__.IE.Y], None]" [builtins fixtures/primitives.pyi] [case testNarrowingWithStrEnum] From f9725b9ce4859f8a66f135f6c6241d555a627198 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 4 Oct 2024 14:04:03 +0100 Subject: [PATCH 2/2] Update test case --- test-data/unit/check-narrowing.test | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index b69cc11d2816..60fc39dd817b 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -2249,6 +2249,14 @@ def f7(x: IE | None) -> None: reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]" else: reveal_type(x) # N: Revealed type is "Union[Literal[__main__.IE.Y], None]" + +def f8(x: IE | None) -> None: + if x is None: + reveal_type(x) # N: Revealed type is "None" + elif x == IE.X: + reveal_type(x) # N: Revealed type is "Literal[__main__.IE.X]" + else: + reveal_type(x) # N: Revealed type is "Literal[__main__.IE.Y]" [builtins fixtures/primitives.pyi] [case testNarrowingWithStrEnum]