Error with TypeVar with constraints on field initialization
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
The following example fails to type-check with mypy and pyright, but succeeds with ty:
from dataclasses import dataclass
from typing import TypeVar, Generic
class X:
pass
class Y(X):
pass
T = TypeVar("T", X, Y)
@dataclass
class C(Generic[T]):
v: T
def f(c: C[T]):
# no error with mypy and ty
# pyright reports an error on the argument
c.v = Y()
# no error with mypy and ty
# pyright reports an error on the assignment
d: C[T] = C(Y())
class M(Generic[T]):
# no error with ty
# mypy and pyright report an error on the assignment
c: C[T] = C(Y())
The constraints on T effectively make Y a lower bound for T, meaning that C(Y()) should be considered a valid C[T] for any allowed T. This seems to be correctly understood by mypy for field assignment and constructor assignment within a function, but not for field initialization at the class level.
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 con l’esempio Python minimale nell’issue e confronta la gestione da parte di mypy delle assegnazioni a TypeVar vincolati all’interno dei corpi delle funzioni con l’inizializzazione dei campi a livello di classe. Riproduci i diversi risultati di mypy, pyright e ty, quindi determina se l’inizializzazione a livello di classe dovrebbe accettare C(Y()) per ogni T consentito e aggiungi la copertura per i casi segnalati.
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