Skip to content

Support [*xs] as a wildcard pattern when matching on Sequence[…] #3304

@LoicRiegel

Description

@LoicRiegel

Summary

Hello, I ran into this (I did quickly check the issues but sorry if this a duplicate):

def main_1(paths: Sequence[str]) -> None:
    match paths:
        case []:
            value = 0
        case [_first]:
            value = 1
        case [_first, _second]:
            value = 2
        case [*_paths]:
            raise ValueError

    print(value)  # error[possibly-unresolved-reference] from ty. OK for mypy.


def main_2(paths: Sequence[str]) -> None:
    match paths:
        case []:
            value = 0
        case [first]:
            value = 1
        case [_first, _second]:
            value = 2
        case [_first, _second, *_paths]:
            raise ValueError

    print(value)  # error[possibly-unresolved-reference] from ty. OK for mypy.

Here all cases are clearly handled, so it would be nice to detect that the variable will always be assigned. Mypy does, apparently.

What's kind of strange is that the following works for ty, but not for mypy:

def main_3(paths: Sequence[str]) -> int:  # mypy error: Missing return statement. OK for ty
    match paths:
        case []:
            return 0
        case [first]:
            return 1
        case [first, second]:
            return 2
        case [first, second, *_paths]:
            raise ValueError

Version

ty 0.0.32 (4d1e1fc 2026-04-20)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions