Repository navigation
Support complete enum literal union equivalence - #11597
Conversation
Co-authored-by: Copilot App <[email protected]>
|
🔒 Automated review in progress — Stella Huang (@StellaHuang95) is auto-reviewing this PR. |
This comment has been minimized.
This comment has been minimized.
|
Separately, the downstream Pylance async evaluator still has the direct-base-only |
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Only concretize top-level TypeVars to probe for a matching enum subtype and fall back to the original type when no enum expansion occurs, so unrelated invariant comparisons like list[T] vs list[ColorLiterals] are unaffected. Adds regression coverage for TypeVar sources in both invariant directions. Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
|
Please mirror the enum expansion and full-MRO |
|
Thanks — agreed the Pylance async evaluator needs the same enum-literal expansion plus the full-MRO This Pyright PR is complete on its own: the expansion + full-MRO/unknown-member exclusion live in |
Co-authored-by: Copilot App <[email protected]>
This comment has been minimized.
This comment has been minimized.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Add parity and Flag match coverage, document union cache construction, and conservatively handle runtime-extensible enum member sets. Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Add @unique regression coverage for the conservative enum member-completeness policy. Co-authored-by: Copilot <[email protected]>
This comment has been minimized.
This comment has been minimized.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Co-authored-by: Copilot App <[email protected]>
|
Diff from mypy_primer, showing the effect of this PR on open source code: sympy (https://github.com/sympy/sympy)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:314:30 - error: Cannot access attribute "as_independent" for class "Basic"
+ Attribute "as_independent" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:380:15 - error: Operator "*" not supported for "None" (reportOptionalOperand)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:13 - error: No overloads for "__init__" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:18 - error: Argument of type "list[Iterator[Unknown]]" cannot be assigned to parameter "iterable" of type "Iterable[list[bytes]]" in function "__init__" (reportArgumentType)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:30 - error: Cannot access attribute "as_independent" for class "Basic"
+ Attribute "as_independent" is unknown (reportAttributeAccessIssue)
+ .../projects/sympy/sympy/solvers/diophantine/tests/test_diophantine.py:383:78 - error: "args" is not a known attribute of "None" (reportOptionalMemberAccess)
- 38290 errors, 82 warnings, 0 informations
+ 38296 errors, 82 warnings, 0 informations
|
| collectLiteralEnumClasses(implementation, literalEnumClasses, recursively, recursionCount); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Warning · Non-blocking recommendation
collectLiteralEnumClasses and normalizeEnumTypes must remain aligned with assignType's recursive shape handling. Add a parity regression using a deeper mixed shape (for example, dict[Color, list[Callable[[Color], Color]]]) through both assignment and assert_type so future walker drift is detected.
|
|
||
| // Concretize top-level TypeVars only to probe for a matching enum subtype. | ||
| // If no enum expansion actually occurs (for example, an unrelated source | ||
| // TypeVar whose bound is not one of the comparison's enum classes), preserve |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
For a source such as T | Color, concretizing the whole source also concretizes the unrelated T; once Color expands, the returned union retains that concretized sibling. Preserve unrelated union members' TypeVars, or add a focused regression test that locks down the intended inference behavior.
| if ( | ||
| ClassType.isEnumClass(classType) && | ||
| (node.d.decorators.length > 0 || | ||
| enumMemberSetMayBeDynamicallyModified || |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
This _missing_ check depends on the current typeshed layout: if a future stub declares _missing_ on IntEnum or StrEnum, complete-union equivalence will silently become unavailable for those enums. Please scope the check to user-defined overrides, or document and test the typeshed assumption explicitly.
| const isInvariant = (flags & AssignTypeFlags.Invariant) !== 0; | ||
| srcType = expandEnumTypeForLiteralComparison(srcType, destType, /* concretizeTypeVars */ !isInvariant); | ||
| if (isInvariant) { | ||
| destType = expandEnumTypeForLiteralComparison(destType, srcType, /* concretizeTypeVars */ false); |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
This normalization applies to every assignType caller, including overload overlap and redundancy analysis. Add a regression test that documents the intended diagnostics for overloads distinguishing a nominal enum from its complete literal union, so this broad semantic change does not cause an accidental future regression.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Summary
Flagenum classes and complete unions of their literal members as contextually equivalent for assignment andassert_typelist/dictand enum-containing union positionsFlag,IntFlag, and indirectFlagsubclasses using full-MRO checksDesign
Enum identity remains nominal by default. The evaluator expands a bare enum only after the destination shape establishes that an enum-literal comparison is needed. A cached union bit makes this destination guard O(1), so unrelated assignments do not enumerate enum symbol tables.
Invariant comparisons normalize both sides locally, which supports reverse substitution such as
list[Literal[Color.RED, Color.GREEN]]tolist[Color]without globally rewriting enum types. Free TypeVar solving runs before expansion, preserving broad enum inference; bound source TypeVars are concretized only within the known literal-union assignment context.assert_typeuses the same expansion primitive and recursively normalizes nested generic, tuple, and union shapes only after ordinary type identity fails.Enum expansion is disabled for full-MRO
Flagancestry, custom or unknown enum metaclasses, and class bodies with assignment targets that cannot be represented in the static class symbol table. This avoids treating a known subset as complete for detectable dynamic member creation while keeping aliases,auto(), and statically named members with unknown values supported.Relation to #11584
This implementation was developed independently from current
mainand does not modify or supersede #11584. It corrects edge cases identified during independent review of that approach:Flag/IntFlagexclusion rather than direct-base-only checksassert_typeequivalence for required nested shapesConformance
The exact current upstream
python/typingconformance/tests/enums_expansion.pywas run with a rebuilt CLI. Before this change, Pyright reported the intentionalFlagdiagnostic at line 56 plus failures at lines 85-86. After this change, output contains exactly the intentional line 56 diagnostic:1 error, 0 warnings, 0 informations.Tests
cd packages/pyright-internal && npm run buildcd packages/pyright-internal && npx jest typeEvaluator3.test -t Enum --forceExit --runInBandtypeEvaluator1.testthroughtypeEvaluator8.test: 1183 tests passedsrc/tests/typeServer: 24 tests passednpm run typechecknpm run check:eslintnpm run check:prettiergit diff --check