python / python/mypy

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

Aperta
#21,537 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug pending topic-match-statement
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l’istruzione match segnalata con MyPy 2.1.0 e 2.0.0 usando lo snippet Python e la configurazione strict mostrati. Leggi il changelog e la PR #21405, quindi traccia la gestione dei capture patterns con tuple string unpacking. Il lavoro è completato quando la regressione è corretta e coperta da un test che dimostri che lo string capture annotato viene accettato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.