Uh oh!
There was an error while loading. Please reload this page.
Exhaustiveness checking for match statements - #12267
Conversation
This comment has been minimized.
This comment has been minimized.
| def remove_capture_conflicts(self, type_map: TypeMap, inferred_types: Dict[Var, Type]) -> None: | ||
| if type_map: | ||
| for expr, typ in type_map.copy().items(): |
There was a problem hiding this comment.
I think list(.items()) is more idiomatic (and probably cheaper) python
| class ProperSubtypeVisitor(TypeVisitor[bool]): | ||
| def __init__(self, right: Type, *, | ||
| ignore_promotions: bool = False, | ||
| ignore_last_known_value: bool = False, |
There was a problem hiding this comment.
Nobody ever passes True here. Is this deadcode?
jhance
left a comment
There was a problem hiding this comment.
Seems fine, but some minor tweaks.
Diff from mypy_primer, showing the effect of this PR on open source code: pylox (https://github.com/sco1/pylox)
- pylox/containers/base.py:62: error: Missing return statement [return]- pylox/containers/array.py:163: error: Missing return statement [return]- pylox/interpreter.py:230: error: Missing return statement [return]- pylox/interpreter.py:258: error: Missing return statement [return] |
tmke8
commented
Mar 3, 2022
This doesn't seem to work: fromenumimportEnum, autoclassE1(Enum):
a=auto()
b=auto()
classE2(Enum):
x=auto()
y=auto()
deff(x: tuple[E1, E2]) ->int:
matchx:
case (E1.a, E2.x):
return0case (E1.a, E2.y):
return1case (E1.b, E2.x):
return2case (E1.b, E2.y):
return3output: but it's probably too difficult to support... |
JukkaL
commented
Mar 7, 2022
Yeah, this seems a bit too complex for now. Since enums are internally expanded into unions of literal types to support exhaustiveness checking, an N-tuple of enums with M items each could be expanded a union of |
Closespython#12010. Mypy can now detect if a match statement covers all the possible values. Example: ``` def f(x: int | str) -> int: match x: case str(): return 0 case int(): return 1 # Mypy knows that we can't reach here ``` Most of the work was done by @freundTech. I did various minor updates and changes to tests. This doesn't handle some cases properly, including these: 1. We don't recognize that `match [*args]` fully covers a list type 2. Fake intersections don't work quite right (some tests are skipped) 3. We assume enums don't have custom `__eq__` methods Co-authored-by: Adrian Freund <adrian@freund.io>
Closes#12010.
Mypy can now detect if a match statement covers all the possible values.
Example:
Most of the work was done by @freundTech. I did various minor updates
and changes to tests.
This doesn't handle some cases properly, including these:
match [*args]fully covers a list type__eq__methods