[ty] Model non-exhaustive enum member sets#26277
Conversation
Typing conformance results improved 🎉The percentage of diagnostics emitted that were expected errors increased from 94.37% to 94.47%. The percentage of expected errors that received a diagnostic increased from 89.00% to 89.10%. The number of fully passing files improved from 94/134 to 95/134. SummaryHow are test cases classified?Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (
Test file breakdown1 file altered
True positives added (1)1 diagnostic
False positives removed (1)1 diagnostic
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
736fe47 to
7e97231
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-return-type |
3 | 0 | 0 |
| Total | 3 | 0 | 0 |
Raw diff:
rotki (https://github.com/rotki/rotki)
+ rotkehlchen/tasks/historical_balances.py:186:20 error[invalid-return-type] Return type does not match returned value: expected `list[tuple[Bucket, Literal[EventDirection.IN, EventDirection.OUT]]]`, found `list[tuple[Bucket, Literal[EventDirection.IN, EventDirection.OUT]] | tuple[Self@from_event, EventDirection]]`
+ rotkehlchen/tasks/historical_balances.py:195:16 error[invalid-return-type] Return type does not match returned value: expected `list[tuple[Bucket, Literal[EventDirection.IN, EventDirection.OUT]]]`, found `list[tuple[Bucket, Literal[EventDirection.IN, EventDirection.OUT]] | tuple[Self@from_event, EventDirection]]`
steam.py (https://github.com/Gobot1234/steam.py)
+ steam/ext/commands/cooldown.py:52:43 error[invalid-return-type] Function can implicitly return `None`, which is not assignable to return type `BucketTypeType`
Merging this PR will not alter performance
Comparing Footnotes
|
| .collect(); | ||
| aliases.sort_unstable(); | ||
| let members_are_exhaustive = !metadata.value_construction.metaclass_may_transform_values | ||
| && !Type::ClassLiteral(class).is_subtype_of(db, KnownClass::Flag.to_subclass_of(db)) |
There was a problem hiding this comment.
This doesn't "finish" support for Flag, but it fell out of optimization work in a separate PR (and turns out to get us passing a new conformance file).
7e97231 to
c031ea2
Compare
9ca4e30 to
b619ccc
Compare
| ``` | ||
|
|
||
| Compact enum complements that are equivalent to a literal union are still spellable. | ||
| When narrowing leaves only one enum member, the inferred type is the corresponding literal. |
There was a problem hiding this comment.
This is an entirely different test now. Why was this changed?
There was a problem hiding this comment.
Because for Flag, the test is actually incorrect!
There was a problem hiding this comment.
I can change the copy back at least though if that's what you mean.
b619ccc to
b7c3250
Compare
Summary
We currently treat the members declared on every enum as its complete runtime domain. That assumption does not hold for
FlagandIntFlag, which also have zero and unnamed combinations, or for enums whose custom_missing_method or metaclass can create additional members. As a result, we can incorrectly treat a one-member enum as a singleton, widen all declared members to the nominal enum, erase a non-empty complement, or consider amatchover the declared members exhaustive.This adds a
members_are_exhaustiveproperty toEnumClassLiteraland uses it wherever we rely on an enum being a finite set: complement construction, union simplification, singleton detection, finite-alternative narrowing, and match exhaustiveness. Open enums retain the nominal remainder after known members are excluded.Explicit membership tests remain precise when the enum uses identity comparison. For example, a metaclass may add
INJECTED, but testing againstONLYstill establishes that exact member on the positive branch and excludes only that member on the negative branch:This is the semantic foundation split out of #26270; that PR adds the compact same-enum comparison path on top.