You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type #21187
Under specific circumstances, changing the order of case-statements in a match-statement influences mypy's type narrowing, leading to flaky assert_never errors.
Specifically, this happens you narrow enum values to specific enum values using Literal and combine that narrowed type with other types in a union.
(I've not done an exhaustive search, but I've only seen this with Literal[SomeEnum.SPECIFIC_VALUE].)
This bug does not occur with mypy==1.19
I've tried to reproduce this bug with Mypy 1.19, but it only occurs with Mypy 1.20. (We ran into this after upgrading Mypy.)
To Reproduce
In the script below, the only different between the dummy_class_then_enum and enum_then_dummy_class is the order of the case-statements within the match statement.
In both instances, all possible value types are handled, but mypy will throw an error on the assert_never in the enum_then_dummy_class function.
changed the title [-][1.20 regression] Type narrowing is sometime order sensitive in match-case statement[/-][+][1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type[/+]on Apr 8, 2026
That commit made mypy's match narrowing use the same code paths as its equality narrowing (which is a correct thing to do). However, mypy's equality narrowing is a little overly conservative when it comes to StrEnum and IntEnum, and that existing deficiency is what is now exposed here
Activity
[-][1.20 regression] Type narrowing is sometime order sensitive in match-case statement[/-][+][1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type[/+]hauntsaninja commentedon Apr 9, 2026
Thanks, bisects to #20146
hauntsaninja commentedon Apr 13, 2026
That commit made mypy's match narrowing use the same code paths as its equality narrowing (which is a correct thing to do). However, mypy's equality narrowing is a little overly conservative when it comes to StrEnum and IntEnum, and that existing deficiency is what is now exposed here
Better narrowing for enums and other types with known equality (#21281)