Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OR pattern structural matching exhaustiveness #18119

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def visit_or_pattern(self, o: OrPattern) -> PatternType:
for pattern in o.patterns:
pattern_type = self.accept(pattern, current_type)
pattern_types.append(pattern_type)
current_type = pattern_type.rest_type
if not is_uninhabited(pattern_type.type):
current_type = pattern_type.rest_type

#
# Collect the final type
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,22 @@ def f(x: int | str) -> int:
case str() as s:
return 1

[case testMatchOrPatternExhaustiveness]
from typing import NoReturn, Literal
def assert_never(x: NoReturn) -> None: ...

Color = Literal["blue", "green", "red"]
c: Color

match c:
case "blue":
reveal_type(c) # N: Revealed type is "Literal['blue']"
case "green" | "notColor":
reveal_type(c) # N: Revealed type is "Literal['green']"
case _:
assert_never(c) # E: Argument 1 to "assert_never" has incompatible type "Literal['red']"; expected "Never"
[typing fixtures/typing-typeddict.pyi]

[case testMatchAsPatternIntersection-skip]
class A: pass
class B: pass
Expand Down
Loading