diff --git a/mypy/checkpattern.py b/mypy/checkpattern.py index 6b4fa35f9c49..4323daa68025 100644 --- a/mypy/checkpattern.py +++ b/mypy/checkpattern.py @@ -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 diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 58b70d7b74d8..3ef964d3c0b9 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -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