Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix negative narrowing of tuples in match statement (#17817)
Fixes #17328 ### Before Lines marked with `!!!` denote incorrect behavior. ([Playground link](https://mypy-play.net/?mypy=1.11.2&python=3.12&flags=strict%2Cwarn-unreachable&gist=7a7081c5fbc2fac9987f24e02421f24f)) ```python from typing import Literal m4: tuple[Literal[1], int] match m4: case (1, 5): reveal_type(m4) # N: Revealed type is "tuple[Literal[1], Literal[5]]" case (1, 6): reveal_type(m4) # !!! E: Statement is unreachable [unreachable] case _: reveal_type(m4) # !!! N: Revealed type is "tuple[Never, builtins.int]" m5: tuple[Literal[1, 2], Literal["a", "b"]] match m5: case (1, str()): reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Union[Literal['a'], Literal['b']]]" case _: reveal_type(m5) # !!! N: Revealed type is "tuple[Literal[2], Never]" match m5: case (1, "a"): reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Literal['a']]" case _: reveal_type(m5) # !!! N: Revealed type is "tuple[Literal[2], Literal['b']]" ``` ### After ```python from typing import Literal m4: tuple[Literal[1], int] match m4: case (1, 5): reveal_type(m4) # N: Revealed type is "tuple[Literal[1], Literal[5]]" case (1, 6): reveal_type(m4) # N: Revealed type is "tuple[Literal[1], Literal[6]]" case _: reveal_type(m4) # N: Revealed type is "tuple[Literal[1], builtins.int]" m5: tuple[Literal[1, 2], Literal["a", "b"]] match m5: case (1, str()): reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Union[Literal['a'], Literal['b']]]" case _: reveal_type(m5) # N: Revealed type is "tuple[Literal[2], Union[Literal['a'], Literal['b']]]" match m5: case (1, "a"): reveal_type(m5) # N: Revealed type is "tuple[Literal[1], Literal['a']]" case _: reveal_type(m5) # N: Revealed type is "tuple[Union[Literal[1], Literal[2]], Union[Literal['a'], Literal['b']]]" ```
- Loading branch information