Fix narrowing of union types containing StrEnum/IntEnum and Literal#20930
Fix narrowing of union types containing StrEnum/IntEnum and Literal#20930bxff wants to merge 3 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
|
I'm fairly certain this can be done with less code. Look at how non- |
|
Also I think we can potentially narrow in the |
When a union contains both StrEnum/IntEnum and Literal/None types, the ambiguity guard in narrow_type_by_identity_equality skips all narrowing. This processes Literal/None union items individually via conditional_types while keeping enum items as-is. Fixes python#20915
f41a44b to
67c6753
Compare
|
Pushed a slimmer revision. @A5rocks Regular enums never hit the ambiguity guard at all ( @ilevkivskyi I did look into if-branch narrowing. Same |
for more information, see https://pre-commit.ci
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Ah I see, I hadn't completely internalized that this is ambiguous. I'll think a bit about whether there's some easy way, but probably this is fine. Edit: Maybe remove this ambiguity check and make |
When a union type contains both StrEnum/IntEnum and Literal/None types (e.g.
Color | Literal["none"]), the ambiguity guard innarrow_type_by_identity_equalityskips all narrowing — even for union members with well-defined equality.This processes Literal/None union items individually via
conditional_typeswhile keeping StrEnum items as-is. The result is built withUnionType.make_unionto avoidmake_simplified_unionabsorbing the StrEnum back intostr.Fixes #20915