mypy
mypy copied to clipboard
Crash in match-statement's subject expression with an unpacked tuple
Crash Report
mypy crashes when it attempts to parse a match-statement with a subject expression that consists of a tuple with an unpacked secondary tuple inside.
See the mypy playground link: https://mypy-play.net/?mypy=latest&python=3.14&flags=show-traceback&gist=e9fee1e1ecabfc2fc0aa5fc7a326f24c
foo = 0
bar = (1, 2)
match foo, *bar:
case _:
pass
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "mypy/checker.py", line 765, in accept
File "mypy/nodes.py", line 2209, in accept
File "mypy/checker.py", line 5968, in visit_match_stmt
File "mypy/checker.py", line 6036, in _get_recursive_sub_patterns_map
AssertionError
main.py:3: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 2.0.0
main.py:3: note: use --pdb to drop into pdb
Your Environment
- Mypy versions used: 1.20.2, 2.0.0, 2.1.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.14.4
- Operating system and version: Arch Linux
As context, in case it matters, the original function where this was noticed was
_TYPE_SOCKET_OPTION: TypeAlias = tuple[int, int, int | bytes]
def _filter_socket_options(sock: socket.socket, options: list[_TYPE_SOCKET_OPTION]) -> Iterator[_TYPE_SOCKET_OPTION]:
for option in options:
match sock.family, *option:
case socket.AF_INET, socket.IPPROTO_IPV6, *_:
pass
case socket.AF_INET6, socket.IPPROTO_IP, *_:
pass
case _:
yield option
until fixed, you can work around this by constructing an unpacked tuple and use that in subject expression:
_TYPE_SOCKET_OPTION: TypeAlias = tuple[int, int, int | bytes]
def _filter_socket_options(sock: socket.socket, options: list[_TYPE_SOCKET_OPTION]) -> Iterator[_TYPE_SOCKET_OPTION]:
for option in options:
sub = sock.family, *option
match sub:
case socket.AF_INET, socket.IPPROTO_IPV6, *_:
pass
case socket.AF_INET6, socket.IPPROTO_IP, *_:
pass
case _:
yield option