Fix type narrowing for equality comparisons with IntEnum and StrEnum values - #11622
Fix type narrowing for equality comparisons with IntEnum and StrEnum values#11622Henry Su (hsusul) wants to merge 1 commit into
Conversation
|
🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR. |
|
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. |
|
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. |
|
The new derived-class path can narrow enum or primitive values to an incompatible literal when no enum member matches, producing unsound types. |
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
| } | ||
| } | ||
| return literalType; | ||
| } |
There was a problem hiding this comment.
Issue · Please address or respond
This bidirectional derived-class check admits all primitive subclasses. For example, x: int compared with Priority.HIGH now narrows to Literal[Priority.HIGH] rather than the primitive literal, and a custom int subclass loses its subtype. IntFlag is also affected: it cannot be enumerated here, so a comparison can fall through to a bare primitive literal. Restrict this cross-class path to supported primitive-backed enum comparisons and preserve the reference subtype when no enum member can be selected.
| val2 = val2.itemType.priv.literalValue; | ||
| } | ||
|
|
||
| if (val1 instanceof EnumLiteral) { |
There was a problem hiding this comment.
Issue · Please address or respond
This shared literal-identity helper now unwraps every EnumLiteral, including ordinary Enum members whose runtime equality does not match their underlying value. Please restrict this behavior to enum classes with primitive runtime equality semantics, or keep it in an equality-specific helper.
| (ClassType.isDerivedFrom(baseLiteralClass, baseSubtypeClass) || | ||
| ClassType.isDerivedFrom(baseSubtypeClass, baseLiteralClass)))); | ||
|
|
||
| if (isSameOrDerivedClass && isClassInstance(subtype)) { |
There was a problem hiding this comment.
Issue · Please address or respond
The symmetric derived-class check is not enum-specific. For example, x: int; if x == True can now narrow x to Literal[True], even though the matching runtime value may be the integer 1. Restrict cross-class compatibility to the supported enum/primitive pairs and add a bool-versus-int regression test.
47b43be to
77c16a9
Compare
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
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)