Skip to content

[2.1 regression] Incompatible types in capture pattern, can't assign a variable when using string unpacking #21537

@WibblyGhost

Description

@WibblyGhost

Bug Report

After upgrading to specifically MyPy 2.1.0 from 2.0.0 on Python 3.13.13 causes a Incompatible types in capture pattern (pattern captures type "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']") [misc] error.

Looking at your release page (https://mypy.readthedocs.io/en/stable/changelog.html) the only particular ticket that looks familiar is #21405.

To Reproduce

def run_funct(cmd: str, *arguments: str) -> None:
    try:
        start_time = time()
        match cmd, *arguments:
            case ("aff", address, "True" | "true" | "False" | "false" as sd):
                address = group_address(address)
                CONSOLE.aff(address, sd=sd.lower() == "true")
            case ("deaff", address):
                address = group_address(address)
                CONSOLE.deff(address)
            case ("set_phase", "TDMA" | "FDMA" | "NATIVE" as phase):  # ERROR: "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']
                CONSOLE.call_rate = call_rate_mapping[phase]

Note: I tried creating a gist in the playground but it doesn't seem to pickup the errors.
Note: This only occurs when as phase is added to set the variable name.

Expected Behavior

MyPy shouldn't be treating the as phase setting as setting to a Literal, or should be able to support comparisons with str. Setting any string through the match statement with tuple[str] unpacking set for a tuple of [str, str] should be allowed to be assigned to a varible

Actual Behavior

mypy src
src/consolesimulator3/scripts/run_console_service.py:78: error: Incompatible types in capture pattern (pattern captures type "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']")  [misc]
Found 1 error in 1 file (checked 34 source files)

Your Environment

[tool.mypy]
exclude = [".*tests/"]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
extra_checks = true
implicit_optional = true
no_implicit_reexport = true
no_warn_no_return = true
strict_equality = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[[tool.mypy.overrides]]
module = ["beaupy.*", "rpyc.*", "construct.*", "sip_parser.*"]
ignore_missing_imports = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions