python / python/mypy

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

Offen
#21,537 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug pending topic-match-statement
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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 https://github.com/python/mypy/pull/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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die gemeldete match-Anweisung mit MyPy 2.1.0 und 2.0.0 unter Verwendung des gezeigten Python-Snippets und der strikten Konfiguration. Lies das Changelog und PR #21405 und verfolge anschließend die Verarbeitung von Capture-Patterns mit Tuple-String-Unpacking. Erledigt ist die Aufgabe, wenn die Regression behoben und durch einen Test abgedeckt ist, der zeigt, dass der annotierte String-Capture akzeptiert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.