Do not narrow Flag enums by member equality - #21942
Open
istoolsfox wants to merge 1 commit into
Open
Conversation
narrow_type_by_identity_equality expands enum instances into a union of member literals and rules out the compared-away member in the negative branch. For enum.Flag subclasses this is unsound: a Flag value can hold any combination of members, so the declared members are not an exhaustive value set. After `programs != Programs.P2`, programs can still be `Programs.P1 | Programs.P2`, so narrowing away the P2 member rejects `Programs.P2 in programs` -- valid at runtime -- against typeshed's `Flag.__contains__(self, other: Self)` (pythongh-21937). Skip equality/identity narrowing when either operand is a Flag value; the sound type for such a value is the enum class itself. Regular enum narrowing is unchanged, and existing Flag bitwise-operator behavior is untouched.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21937
Description
narrow_type_by_identity_equality(used for==/!=/is/is notnarrowing) expands an enum-typed expression into a union of member literals and, in the negative branch, rules out the compared-away member. Forenum.Flagsubclasses this is unsound: a Flag value can hold any combination of members, so the declared members are not an exhaustive value set.The reported repro:
After the two
==checks, mypy narrowsprogramstoLiteral[Programs.P1, Programs.P3, Programs.ALL]-- a type that a runtime value likePrograms.P1 | Programs.P2cannot inhabit. The subsequentPrograms.P2 in programsis then rejected: typeshed definesFlag.__contains__(self, other: Self), and no member of the narrowed union accepts aP2value, even thoughPrograms.P2 in Programs.P1 | Programs.P2isTrueat runtime.This change skips equality/identity narrowing when either operand is a Flag value (an instance of, or a member of, an
enum.Flag/enum.IntFlagsubclass): the sound type for such a value is the enum class itself. Narrowing for regular enums is unchanged, since their members do form an exhaustive value set.Checklist
check-enum.test; they fail without the fixtestcheck.pysuite passes locally (8207 passed, 15 skipped, 7 xfailed);check-narrowing,check-expressions,check-literal,check-unreachable-codealso passmypy --config-file mypy_self_check.inipasses on the touched modulesruff check/ruff format --check(pinned ruff 0.14.3) pass on the touched modules