Incorrect type narrowing for `x in y` pattern when `x` is `type[T]` and `y` is an iterable of class objects
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
As the title suggests, if one uses the type_var in (type1, type2, ...) construct instead of issubtype(type_var, (type1, type2, ...), and type_var is generic, mypy struggles with narrowing down the type.
The same bug happens in pyright.
To Reproduce
from typing import TypeVar
T = TypeVar('T')
def func(typ: type[T], input: str) -> T:
if typ in (str, int, float):
return typ(input) # error: Too many arguments for "object" [call-arg]
else:
raise ValueError() # Just for the sake of demonstration
def func(typ: type[T], input: str) -> T:
if issubclass(typ, (str, int, float)):
return typ(input) # No problem!
else:
raise ValueError() # Just for the sake of demonstration
Expected Behavior
Both versions of the type check act the same.
Actual Behavior
Mypy incorrectly throws Too many arguments for "object" [call-arg] error.
Your Environment
- Mypy version used: 1.11.2
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12 / 3.10
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il riproduttore fornito con mypy e confronta i casi in e issubclass. Segui il percorso del restringimento dei tipi per i controlli di appartenenza e aggiungi un test di regressione che copra type[T] con un iterabile di oggetti classe; il lavoro è completato quando la chiamata typ(input) viene accettata nel caso in senza indebolire il comportamento esistente.
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à
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100