no error on `TypeVar` with explicitly declared variance when the generic is used in another type with a different variance
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
# mypy: enable-incomplete-feature=NewGenericSyntax
# mypy: disable-error-code=empty-body
from typing import Generic, TypeVar
class Contravariant[T]:
def fn(self, value: T) -> None:
...
class Foo[T]:
def fn(self) -> Contravariant[T]: ...
def call_foo_with_string(foo: Foo[object]) -> None:
foo.fn().fn("")
foo_number = Foo[int]()
call_foo_with_string(foo_number) # correct error: Argument 1 to "call_foo_with_string" has incompatible type "Foo[int]"; expected "Foo[object]"
out_T = TypeVar('out_T', covariant=True)
class Bar(Generic[out_T]):
def fn(self) -> Contravariant[out_T]: ...
def call_bar_with_string(bar: Bar[object]) -> None:
bar.fn().fn("")
bar_number = Bar[int]()
call_bar_with_string(bar_number) # no error (incorrect)
in this example, the variance rules are inverted when the generic is passed to another class where it becomes contravariant (as in, the generic must now only be used in an output position)
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 Python fornito con mypy e confronta i casi NewGenericSyntax e TypeVar/Generic. Traccia come viene verificata la varianza dichiarata esplicitamente quando il generico è annidato in Contravariant. Il lavoro è completato quando la seconda chiamata segnala la stessa incompatibilità della prima, con una copertura di regressione per questo riproduttore.
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