Bug Report
If you compare an enum to a literal, then the type becomes that literal. Somewhat a followup to #19594.
To Reproduce
from enum import Enum class A(str, Enum): a = "b" b = "a" def test(a: A) -> None: if a == "b": reveal_type(a)
Expected Behavior
main.py:9: note: Revealed type is "Literal[A.a]" Success: no issues found in 1 source file
Actual Behavior
main.py:9: note: Revealed type is "Literal['b']" Success: no issues found in 1 source file
Your Environment
Reproduced in mypy playground.
Interestingly, if there's only 1 enum member, then the reveal_type becomes unreachable? No matter what the enum member is.
if there's only 1 enum member
with #19594 it's handled better: still Literal['b'] if b is among enum values, unreachable otherwise.
Also this problem does not exist for StrEnum, only (str, Enum) / (int, Enum) subclasses are affected.
Improve narrowing logic for Enum int and str subclasses (#20609)
Activity
A5rocks commented on Aug 28, 2025
Interestingly, if there's only 1 enum member, then the reveal_type becomes unreachable? No matter what the enum member is.
sterliakov commented on Aug 29, 2025
with #19594 it's handled better: still Literal['b'] if b is among enum values, unreachable otherwise.
Also this problem does not exist for StrEnum, only (str, Enum) / (int, Enum) subclasses are affected.
Improve narrowing logic for Enum int and str subclasses (#20609)