Repository navigation
Do not narrow enum equality comparisons when member values are not statically known - #11692
Nilesh Patil (nileshpatil6) wants to merge 4 commits into
Conversation
|
🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR. |
|
Please add the direct Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. Result: Summary: Verification could not run because no trusted sandbox image is configured for `microsoft/pyright`, and local execution was not authorized. The PR adds five scenarios to the existing `EnumNarrowing1` sample test, covering unknown `auto()` and computed enum values in both comparison directions. No executable test result was obtained. Test runs: 1 not run
|
Bill Schnurr (bschnurr)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
|
Added the match statement cases in 50ff887.
Both assert that neither branch narrows. On main they report errors (the matching case eliminates the unknown-valued member); with the change the sample is clean.
|
|
Verification: The relevant tests could not be fully run in the isolated environment; this review is not fully verified. Result: Summary: No tests executed because the verification container could not start: no trusted `microsoft/pyright` sandbox image was configured, and local execution was unauthorized. The PR adds seven scenarios to the existing `EnumNarrowing1` sample covering unknown `IntEnum` values, reverse comparisons, computed values, and match narrowing. Runtime confidence is therefore limited. Test runs: 1 not run
|
Heejae Chang (heejaechang)
left a comment
There was a problem hiding this comment.
Approved via Review Center.
🟢 Pyright CLI QA —
|
|
The added The distinction is between a direct pattern and an equality comparison in a guard: # Direct literal pattern: uses the separate pattern-matching narrowing path.
match value:
case 40:
...
# Equality in a guard: exercises the equality helper changed by this PR.
match value:
case _ if value == 40:
...Could you clarify which match behavior this PR fixes and add a regression test that fails on the baseline and passes with the change? If the direct-pattern tests are intended as unchanged-behavior controls, keeping them makes sense, but the description should identify them as such. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Copilot resolve the merge conflicts in this pull request |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
The direct computed-enum comparison is fixed, but a nested computed value still gets its reachable branch discarded:
from enum import IntEnum
from typing import Literal
def computed() -> int: return 7
class Source(IntEnum): VALUE = computed()
class Nested(IntEnum): VALUE = Source.VALUE
def nested(v: Literal[Nested.VALUE]) -> int:
if v == 7:
return 'reachable' # head misses reportReturnType
return 0Both base and head consider the branch unreachable and miss reportReturnType, but CPython 3.14.3 reaches it (Nested.VALUE == 7 is true). The direct Source.VALUE == 7 control is incorrectly discarded on base and checked on head. A known nonmatch remains unreachable.
unwrapPrimitiveBackedEnumLiteral treats the inner EnumLiteral as a known literal even when its primitive payload is unknown. Could we conservatively preserve unknown nested values and add coverage?
test_int_var_equals_auto_member also passes on base because its positive branch becomes Never and the assertion never gets checked. On base, the sample reports errors only at lines 103, 128 and 137, none in this test. This local form fails on base (Never) and passes on head:
from enum import IntEnum, auto
from typing import Literal, assert_type
class AutoValue(IntEnum):
RED = auto()
GREEN = auto()
def local_form(flag: bool):
x = 1 if flag else 3
if x == AutoValue.RED:
assert_type(x, Literal[1, 3])Could we add a reachability-sensitive reverse-direction test like this?
One optional precision improvement: for C(IntEnum) with FIRST = get_int(), SECOND = 40 and THIRD = 50, head keeps all of C after v: C is compared to 40. The expanded Literal[C.FIRST, C.SECOND, C.THIRD] narrows to Literal[C.FIRST, C.SECOND]. Keeping all of C is sound. Base was unsound if FIRST evaluates to 40, since SECOND becomes its alias. Maybe the C form could keep definite matches and unknown-valued members while dropping known nonmatches. That refinement hasn't been tried.
|
AI-assisted review. Following up on the precision case in Rich Chiodo (@rchiodo)'s review: retaining the entire enum also causes a new false positive at an ordinary consumer. from enum import IntEnum
from typing import Literal, reveal_type
def computed() -> int:
return 2
class E(IntEnum):
ONE = 1
COMPUTED = computed()
THREE = 3
def consume(value: Literal[E.ONE, E.COMPUTED]) -> str:
return value.name
def example(value: E) -> str:
if value == 1:
reveal_type(value)
return consume(value)
return "other"
if __name__ == "__main__":
for member in E:
print(example(member))In basic mode, baseline Could the positive branch retain the definite matches and unknown-valued members, while excluding known nonmatches, with a consumer test like this one? |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Class B: Pyright cannot determine values added or changed by dynamic enum hooks. Keep those comparisons conservative while retaining the possible literal members for standard enums and preserving TypeVar conditions. Treat nested unknown enum values as indeterminate. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Pushed d70e5f6 to cover nested enum members whose underlying values are unknown and to preserve useful positive-branch precision for partially known enums. For standard construction and comparison, narrowing keeps all possible matching members; for dynamic hooks or custom behavior it conservatively retains the enum. The change also preserves TypeVar conditions. Validation: the core build, ESLint, Prettier, and all eight evaluator suites passed (1,463 tests), including the focused default and Python 3.11 enum cases. Dynamic subclass and descriptor hooks were checked against CPython. The additional enum-member scan has not been performance-benchmarked. Please update the PR description: an indeterminate member no longer necessarily prevents all positive-branch narrowing, and the follow-up adds |
|
Thanks Bill Schnurr (@bschnurr) for the follow-up commit, and Rich Chiodo (@rchiodo) for the nested and consumer cases. I've updated the description to match the new positive-branch behaviour and the |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Compared candidate Release history charts were not rendered for this run. See the workflow run for details. Type checker benchmark🟢 No performance regressions detected. Regression threshold:
Pyright stats
|
Bug
#11622 added equality narrowing for primitive-backed enums, so a member of an
IntEnumorStrEnumcorrectly compares equal to its underlying value. When a member's value is not statically known, though,unwrapPrimitiveBackedEnumLiteralfalls back to returning the enum literal unchanged.isLiteralValueEqualAtRuntimethen compares anEnumLiteralagainst anintorstrliteral, which is never equal, so the comparison is treated as statically false and the positive branch is eliminated.auto()is the common way to hit this:The same happens in reverse (
x: Literal[1, 3]compared againstx == Color.RED), and in a match statement a case on a member whose value is known can unsoundly eliminate siblings whose values are not.Any computed member value has the same effect,
auto()is just the most common form.Change
isLiteralValueEqualAtRuntimenow returnsboolean | undefined, whereundefinedmeans the comparison cannot be decided statically because a member's underlying value is unknown. This includes nested members whose value comes from another enum member with an unknown value.An indeterminate member no longer blocks all positive-branch narrowing. For standard enum construction and comparison, the positive branch keeps every member that could match (definite matches plus members with unknown values) and still excludes known nonmatches, so partially known enums keep useful precision. Where custom
__eq__/__ne__/__new__, a custom metaclass, or class creation hooks could change the result, narrowing conservatively retains the enum. TypeVar conditions are preserved.All determinate behaviour from #11622 is unchanged: enums whose member values are all known narrow exactly as before.
Tests
enumNarrowing1.py:auto()and computed members in both comparison directions, plus the match-statement sibling-elimination case.enumNarrowingSafety1.py(added by Bill Schnurr (@bschnurr) in d70e5f6): mixed known/unknownIntEnumandStrEnumwith narrower consumers, inequality, nested and deeply nested unknown values, known nested nonmatches, custom equality/construction/metaclass,IntFlag, subclass and descriptor hooks, and bound/constrained TypeVars.Core build, ESLint, Prettier and all eight evaluator suites pass (1,463 tests).