Repository navigation
Do not expand Flag enums into a union of member literals - #21942
istoolsfox wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
| return isinstance(typ, LiteralType) or is_singleton_identity_type(typ) | ||
|
|
||
|
|
||
| FLAG_ENUM_BASES: Final = ("enum.Flag", "enum.IntFlag") |
hauntsaninja
left a comment
There was a problem hiding this comment.
This isn't an issue with narrowing, it's an issue with handling of __contains__
def f(p: Literal[Programs.ALL]) -> None:
if Programs.P2 in p:
pass
The problem is with narrowing after is/== (it throws an error where there shouldn’t be any). The absence of narrowing after in only prevents some errors from being raised. from enum import Flag, auto
from typing import reveal_type
class F(Flag):
A = auto()
B = auto()
def f(x: F):
reveal_type(x) # all: F
if x is F.A:
reveal_type(x) # all: Literal[F.A]
return
reveal_type(x) # pyright, pyrefly, mypy (with this PR): F, mypy(now): Literal[F.B], ty: F & ~Literal[F.A]
def g(x: F):
reveal_type(x) # all: F
if F.A in x:
reveal_type(x) # all: F
return
reveal_type(x) # all: F |
|
We should just not expand a flag enum to a Union (it would still be valid to narrow a Union of explicitly listed flag literals). And separately, the |
|
Should be in |
A Flag value can hold any combination of members, so the declared members are not an exhaustive set of values. Expanding a Flag enum into a union of member literals is therefore unsound: after `x is not F.A`, values such as `F.A | F.B` are still possible and must not be pruned. Skip the expansion for Flag subclasses in try_expanding_sum_type_to_union(). Enums whose members form a closed set still expand, and narrowing a union that was written out explicitly still removes members. Fixes python#21937
42c9149 to
302bab1
Compare
|
You're right -- I was fixing a symptom. Reworked along the lines you suggested:
On the p: Literal[Programs.P2]
Programs.P1 in p # still an error: Self is bound to Literal[Programs.P2]The same binding also makes |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #21937
Description
try_expanding_sum_type_to_union()no longer expandsenum.Flagsubclasses into a union ofmember literals.
A Flag value can hold any combination of members, so the declared members are not an exhaustive
set of values, and turning the enum into a union of member literals is unsound. In the reported
case:
the guards narrowed
programstoLiteral[Programs.P1, Programs.P3, Programs.ALL]-- a typethat a value such as
Programs.P1 | Programs.P2cannot inhabit. The union then surfaced as anerror on
in, becauseFlag.__contains__(self, other: Self)has no member in that union thataccepts
Programs.P2.Regular enums keep expanding, and narrowing a union that was written out explicitly still
removes members:
Positive narrowing for Flag is preserved (
if programs is Programs.P1->Literal[Programs.P1]);only the negative branch stays at the enum class, since mypy cannot spell
Programs & ~Literal[Programs.P1].Checklist
check-enum.test; both fail without the change (testFlagEnumNotExpandedIntoUnion)or guard against overreach (
testFlagEnumExplicitLiteralUnionStillNarrows)testcheck: 8314 passed, 34 skipped, 7 xfailedtestfinegrained: 756 passed, 27 skippedmypy --config-file mypy_self_check.ini -p mypy -p mypycon the touched treeruff check/ruff format --checkon the touched filesNot in this PR
__contains__needs a separate fix: when the receiver is a member literal(
p: Literal[Programs.P2]; Programs.P1 in p),SelfinFlag.__contains__(self, other: Self)is still bound to the member literal. That is tracked separately from #21937.