python / python/mypy

[1.20 regression] value pattern match statement narrowing broken for typevars with values

Aperta
#21,356 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report
I think I've found a regression in 1.20 - was recently trying to migrate a larger codebase from 1.18 and noticed this issue in a few places which didn't make sense. I've isolated it down to a small example which is a bit silly, but hopefully clear. I think this is a consequence of a deliberate change in https://github.com/python/mypy/pull/20908. Happy to try and put together a PR to resolve if useful, but I'm not exactly across the mypy internals, so might need a bit of guidance.

To Reproduce
https://mypy-play.net/?mypy=1.20.2&python=3.12&gist=ae5055b7e174f0ffd34db390a192aef5


import builtins
from typing import TypeVar

T = TypeVar("T", int, str)

def f(x: T) -> T:
    match type(x):
        case builtins.int:
            return 42        # mypy 1.20: error: Incompatible return value type (got "int", expected "str")
        case builtins.str:
            return "hello"   # mypy 1.20: error: Incompatible return value type (got "str", expected "int")
        case _:
            raise TypeError

Expected Behavior
Expected there to be no issues
Actual Behavior

uv run --with mypy==1.20.2 mypy mre.py 
mre.py:24: error: Incompatible return value type (got "int", expected "str")  [return-value]
mre.py:26: error: Incompatible return value type (got "str", expected "int")  [return-value]

edit: I did a bit more testing, there's nothing special about builtins here, but it needs to be a value pattern and not a class pattern for this example to even work in 1.19

Your Environment

  • Mypy version used: 1.20.2 fails, 1.19 works, a quick bisect suggests afb99ebbc is where this changes
  • Mypy command-line flags: None in minimal example
  • Mypy configuration options from mypy.ini (and other config files): None in minimal example
  • Python version used: 3.13 / 3.12 in playground link above

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

Inizia con l'esempio minimo in mre.py e confronta mypy 1.19 con 1.20.2, usando il commit afb99ebbc come punto di regressione segnalato. Traccia il narrowing dei pattern di valori per i TypeVars vincolati e aggiungi un test di regressione; il lavoro è completato quando l'esempio non produce errori di valori restituiti incompatibili.

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

Valutazione

Stack tecnologico
python
Ambito
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.