Repository navigation
Support enum assignment to complete literal unions - #11584
Kirill (vetrovk) wants to merge 5 commits into
Conversation
|
🔒 Automated review in progress — Rich Chiodo (@rchiodo) is auto-reviewing this PR. |
| } | ||
| } | ||
|
|
||
| if ( |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
This normalizes only when an entire asserted or actual type is a bare enum instance. assert_type(value, Literal[E.A, E.B] | int) still fails for value: E | int, even though the corresponding union assignment expands its enum subtype. Please normalize enum members within unions here as well, and add coverage for this shape.
| } | ||
| } | ||
|
|
||
| if ( |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
The guarded enum-to-literal-union expansion is now duplicated for both assert_type operands and the assignType source. Please consider centralizing this normalization helper so future enum edge-case adjustments cannot drift across the three copies.
| constraints, | ||
| flags, | ||
| recursionCount | ||
| ); |
There was a problem hiding this comment.
Info · Optional note
A single-member enum's complete literal-member union is represented as a lone Literal[Single.ONLY], not a UnionType, so this union-only branch never runs. single_value: Literal[Single.ONLY] = broad_single_enum therefore remains rejected. Please decide whether the intended rule includes this degenerate complete union and add a regression test.
|
|
||
| a = TestEnum1["A"] | ||
| aa = TestEnum1.A | ||
| reveal_type(aa.name, expected_text="Literal['A']") |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
Please add boundary coverage for TypeVar/Optional inference (for example, reveal the result of def f[T](x: T | None) -> T called with a broad enum) and for single-member enum assignment and assert_type. These cases exercise the constraint-solving and non-union destination boundaries of the new normalization.
|
When this change is integrated into pyrx, mirror the enum normalization in Pylance's separate async evaluator to preserve sync/async parity. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
|
||
| const expandedEnumSrcType = expandEnumTypeToLiteralUnion(srcType); | ||
| if (expandedEnumSrcType !== srcType) { | ||
| return assignType(destType, expandedEnumSrcType, diag, constraints, flags, recursionCount); |
There was a problem hiding this comment.
Issue · Please address or respond
Expanding the source before checking the assignment context breaks invariant type-argument comparisons: list[Color] to list[Color], dict[Color, int] to itself, and list[Color | int] to itself now fail because the source becomes a literal union while the destination remains Color. Please preserve identity for invariant comparisons while enabling the intended enum-to-complete-literal-union widening, and add regressions for these container self-assignments.
| assert_type(literal_value, TestSingleMemberEnum) | ||
|
|
||
|
|
||
| def test_enum_without_known_members(value: Enum) -> None: |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
The added test_generic_enum_literal_union covers class-generic container assignment, not constraint solving through T | None. Please add a regression such as def f[T](x: T | None) -> T called with a multi-member enum, asserting the revealed type remains TestEnum3 rather than a literal union. This pins the intended boundary that enum expansion does not leak into TypeVar inference.
[verified]
| } | ||
|
|
||
| const expandedEnumSrcType = expandEnumTypeToLiteralUnionForDestination(srcType, destType); | ||
| if (expandedEnumSrcType !== srcType) { |
There was a problem hiding this comment.
Info · Optional note
This executes before checking the destination shape, so every assignment from a bare enum source enumerates its members even when the destination cannot accept the resulting literal union. Since assignType is a hot path, add a cheap precondition that permits union destinations and single literal destinations, while preserving the single-member-enum case.
[verified]
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
This comment has been minimized.
This comment has been minimized.
|
The mypy-primer found a bunch of differences. Can you double check those are expected with the new change? Sympy ones can usually be ignored since that seems to be flakey but the other ones look like real differences. |
| const expandedEnumSrcType = expandEnumTypeToLiteralUnionForDestination(srcType, destType); | ||
| if (expandedEnumSrcType !== srcType) { | ||
| return assignType(destType, expandedEnumSrcType, diag, constraints, flags, recursionCount); | ||
| } |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
This hot-path expansion enumerates every member of a bare enum even when the destination cannot be its literal union (for example, enum-to-same-enum assignments). Add a cheap destination precondition before calling expandEnumTypeToLiteralUnionForDestination, while retaining support for a single literal destination so the single-member-enum case continues to work.
[verified]
|
|
||
|
|
||
| class TestFlagLiteralUnion(Flag): | ||
| A = auto() |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
Please add a T | None constraint-solving regression case that passes a multi-member enum and verifies T remains the bare enum type. The current generic-container test exercises invariant type arguments, but it does not pin this separate inference boundary against future expansion leakage.
[verified]
|
The single-member-enum narrowing is an intentional behavior change beyond the main assignment fix; document that effect explicitly in the PR description. |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
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)
+ .../projects/sympy/sympy/solvers/ode/hypergeometric.py:247:67 - error: Operator "**" not supported for types "Basic" and "Literal[2]" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/lie_group.py:619:61 - error: Operator "-" not supported for type "Unknown | Basic" (reportOperatorIssue)
- .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:468:28 - error: Argument of type "Unknown | None" cannot be assigned to parameter "expr" of type "Expr" in function "make_args"
+ .../projects/sympy/sympy/solvers/ode/nonhomogeneous.py:468:28 - error: Argument of type "Expr | Unknown | None" cannot be assigned to parameter "expr" of type "Expr" in function "make_args"
- Type "Unknown | None" is not assignable to type "Expr"
+ Type "Expr | Unknown | None" is not assignable to type "Expr"
+ .../projects/sympy/sympy/solvers/ode/ode.py:1579:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1580:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1590:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1590:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1590:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
+ Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
+ "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
+ "__iter__" is not present (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1597:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1597:64 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1603:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1603:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1603:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
+ Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
+ "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
+ "__iter__" is not present (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1610:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1610:74 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1616:9 - error: No overloads for "update" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1616:12 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1616:19 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
+ Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
+ "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
+ "__iter__" is not present (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1623:49 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:1623:70 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
- .../projects/sympy/sympy/solvers/ode/ode.py:1753:36 - error: Cannot access attribute "lhs" for class "Expr"
- Attribute "lhs" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1753:55 - error: Cannot access attribute "rhs" for class "Expr"
- Attribute "rhs" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1754:36 - error: Cannot access attribute "lhs" for class "Expr"
- Attribute "lhs" is unknown (reportAttributeAccessIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1755:17 - error: No overloads for "__setitem__" match the provided arguments (reportCallIssue)
- .../projects/sympy/sympy/solvers/ode/ode.py:1755:17 - error: Argument of type "Equality | BooleanFalse | BooleanTrue | Unknown | Expr" cannot be assigned to parameter "value" of type "Equality | BooleanFalse | BooleanTrue" in function "__setitem__"
- Type "Equality | BooleanFalse | BooleanTrue | Unknown | Expr" is not assignable to type "Equality | BooleanFalse | BooleanTrue"
- Type "Expr" is not assignable to type "Equality | BooleanFalse | BooleanTrue"
- "Expr" is not assignable to "Equality"
- "Expr" is not assignable to "BooleanFalse"
- "Expr" is not assignable to "BooleanTrue" (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3442:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3442:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3443:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3443:38 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3444:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3445:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3446:14 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3458:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3459:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3460:50 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3500:5 - error: No overloads for "update" match the provided arguments (reportCallIssue)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3500:7 - error: "update" is not a known attribute of "None" (reportOptionalMemberAccess)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3500:14 - error: Argument of type "Unknown | dict[Unknown, Unknown] | None" cannot be assigned to parameter "m" of type "Iterable[tuple[str, Unknown]]" in function "update"
+ Type "Unknown | dict[Unknown, Unknown] | None" is not assignable to type "Iterable[tuple[str, Unknown]]"
+ "None" is incompatible with protocol "Iterable[tuple[str, Unknown]]"
+ "__iter__" is not present (reportArgumentType)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3501:18 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3501:43 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3502:9 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3502:16 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3502:24 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3502:31 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3503:9 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3503:15 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
+ .../projects/sympy/sympy/solvers/ode/ode.py:3503:23 - error: Object of type "None" is not subscriptable (reportOptionalSubscript)
... (truncated 704 lines) ...
discord.py (https://github.com/Rapptz/discord.py)
+ .../projects/discord.py/discord/ext/commands/parameters.py
+ .../projects/discord.py/discord/ext/commands/parameters.py:129:34 - warning: Unnecessary "# type: ignore" comment (reportUnnecessaryTypeIgnoreComment)
- 4 errors, 9 warnings, 0 informations
+ 4 errors, 10 warnings, 0 informations
|
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
Stella Huang (StellaHuang95)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
|
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
| function expandEnumTypeToLiteralUnion(type: Type): Type { | ||
| return mapSubtypes(type, (subtype) => { | ||
| if (isClassInstance(subtype) && subtype.priv.literalValue === undefined && ClassType.isEnumClass(subtype)) { | ||
| const expandedLiteralTypes = enumerateLiteralsForType(evaluatorInterface, subtype); |
There was a problem hiding this comment.
Issue · Please address or respond
enumerateLiteralsForType excludes only direct Flag bases, so IntFlag and indirect Flag subclasses can be expanded and accepted as exhaustive literal unions even though combined values remain valid. Exclude the complete Flag inheritance family and add IntFlag plus indirect-subclass regressions.
| return true; | ||
| } | ||
|
|
||
| const expandedEnumSrcType = expandEnumTypeToLiteralUnionForDestination(srcType, destType); |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
This runs in the central assignType path and enumerates broad enum sources before determining whether the destination can contain the complete literal union. Please add a cheap destination-shape gate or demonstrate that this enumeration is sufficiently cached for enum-heavy workloads.
| return true; | ||
| } | ||
|
|
||
| const expandedEnumSrcType = expandEnumTypeToLiteralUnionForDestination(srcType, destType); |
There was a problem hiding this comment.
Warning · Non-blocking recommendation
Pylance has a structurally separate async evaluator counterpart for this logic. Before this change is vendored into Pylance, ensure that implementation receives equivalent normalization or add TSP-mode parity coverage.
|
Thanks for the detailed review. I checked the latest main before applying the follow-up changes and found that #11597 has now been merged and covers the same enum literal expansion behavior along with the Flag/IntFlag, destination gating, TypeVar, invariant container, and narrowing cases discussed here. |
Fixes #11573.
The typing specification treats a non-Flag enum as equivalent to the union of all of its literal members.
Pyright already handled assignment from an enum literal union to the enum type, but the reverse direction failed because a broad enum source was tested independently against each destination literal.
Expand non-literal enums to their complete literal-member union before union assignability checks. Apply the same normalization symmetrically for
assert_type.The change preserves existing behavior for:
enum.FlagintandstrliteralsTests cover ordinary Enum, IntEnum, StrEnum, the generic case from the issue, symmetric
assert_typechecks, incomplete unions, Flag, and underlying literals.Validation:
typeEvaluator3.test.ts: 172/172 passednpm run build:cli:devgit diff --check