Fix type narrowing for equality comparisons with IntEnum and StrEnum values - #11622
Open
Henry Su (hsusul) wants to merge 1 commit into
Open
Fix type narrowing for equality comparisons with IntEnum and StrEnum values#11622Henry Su (hsusul) wants to merge 1 commit into
Henry Su (hsusul) wants to merge 1 commit into
Conversation
Collaborator
|
🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR. |
Collaborator
|
The expanded derived-class gate can narrow ordinary bool/int comparisons to the wrong primitive type, and unmatched enum comparisons can produce invalid primitive literals. These are soundness regressions in shared type-narrowing code. |
Collaborator
|
The derived-class widening introduces unsound narrowing for ordinary bool/int comparisons and for enum comparisons with unmatched literals. These regressions can change variables to incompatible primitive literal types. |
Collaborator
|
The new derived-class path can narrow enum or primitive values to an incompatible literal when no enum member matches, producing unsound types. |
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.
Summary
Fixes type narrowing for equality (
==and!=) comparisons betweenIntEnum/StrEnumvalues and primitive literals (or literal unions).Python Reproduction & Expected Behavior
In Python,
IntEnum(derived fromintandenum.Enum) andStrEnum(derived fromstrandenum.Enum) are runtime subtypes ofintandstr. Equality comparisons such asp == 20(wherep: Priority) orx == Priority.HIGH(wherex: intorx: Union[Literal[10], Literal[20]]) evaluate toTrueat runtime when the values match.Root Cause & Implementation
narrowTypeForLiteralComparison(packages/pyright-internal/src/analyzer/typeGuards.ts), line 2714 previously checkedClassType.isSameGenericClass(literalType, subtype). When comparing a derived enum class (e.g.PriorityorStatus) to an underlying primitive type (intorstr),isSameGenericClassreturnedfalse, causing Pyright to skip literal comparison and returnsubtypeun-narrowed.isSameOrDerivedClassusing the un-literal base class types (ClassType.cloneWithLiteral(..., undefined)) when evaluating equality (!isIsOperator) tests.ClassType.isLiteralValueSame(packages/pyright-internal/src/analyzer/types.ts), comparingEnumLiteraltonumberorstringreturnedfalse.isLiteralValueSameto un-wrapval1.itemType.priv.literalValue(the underlying primitive literal value forIntEnum/StrEnum/ReprEnummembers) when comparing with primitive literals.Regression Coverage
packages/pyright-internal/src/tests/samples/enumNarrowing1.pyand registeredEnumNarrowing1test case intypeEvaluator3.test.ts.Validation Results
npx jest src/tests/typeEvaluator3.test.ts -t "EnumNarrowing1": PASSnpx jest src/tests/typeEvaluator1.test.ts ... typeEvaluator8.test.ts: PASS (1208/1208 tests passed)npm run test): PASS (64/64 test suites, 2578/2578 tests passed)npm run check): PASSnpm run typecheck): PASSnpm run build:cli:dev): PASSgit diff --check): PASS (clean)