Skip to content

[match-case] unreachable raised multiple times instead of once. #15866

Open
@randolf-scholz

Description

@randolf-scholz

Bug Report

unreachable is raised multiple times in match-case.

To Reproduce

from typing import Any
import re

def foo(config: dict[str, Any], section: str) -> set[str]:
    """Extract dependencies from pyproject.toml."""
    try:
        for key in section.split("."):
            config = config[key]
    except KeyError:
        return NotImplemented
    
    reveal_type(config)  # dict[str,Any], should be dict[str,Any] | Any
    
    match config:
        # assume format `"package<comparator>version"`
        case list() as lst:  # ✘ unreachable
            regex = re.compile(r"[a-zA-Z0-9_-]*")   # ✘ unreachable
            return {re.search(regex, dep).group() for dep in lst}
        # assume format `package = "<comparator>version"`
        case dict() as dct:
            return set(dct.keys())       
        case _:
            raise TypeError(f"Unexpected type: {type(config)}")

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict%2Cwarn-unreachable&gist=d747fcb30a23bd50025c2ca47ed8c5cf

Expected Behavior

A single type: ignore[unreachable] on line case list() as lst: should suffice to silence both messages / there should only be a singular unreachable error to begin with.

Actual Behavior

main.py:16: error: Subclass of "dict[str, Any]" and "list[Any]" cannot exist: would have incompatible method signatures  [unreachable]
main.py:17: error: Statement is unreachable  [unreachable]
Found 2 errors in 1 file (checked 1 source file)

with the added type: ignore on line 16 it's still

main.py:17: error: Statement is unreachable  [unreachable]
Found 1 error in 1 file (checked 1 source file)

EDIT: moved comment in code.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions