Type of argument is not inferred for a TypeVar in a Generic type
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 needs a type annotation for a variable that it could easily infer from the constructor argument annotations. This works perfectly with normal types. But using a TypeVar in a Generic type, somehow it doesn't.
from typing import Generic, TypeVar
class A:
pass
class B:
pass
T_Value = TypeVar("T_Value", A, B)
class Guess(Generic[T_Value]):
def __init__(self, value: T_Value, confidence: float) -> None:
self.value = value # <-- line 17
self.confidence = confidence
Mypy then complains it needs a type annotation
foo.py:17: error: Need type annotation for 'value'
This of course is no big deal since I can just add that annotation:
class Guess(Generic[T_Value]):
def __init__(self, value: T_Value, confidence: float) -> None:
self.value: T_Value = value
self.confidence = confidence
But shouldn't mypy infer the type from the type annotations in the constructor?
If I'm doing something wrong please let me now.
I didn't find a similar issue but if this is a duplicate feel free to close.
I'm using:
Python 3.7.0, mypy 0.610, macOS 10.13.5
Same result with the current master (2018-07-05):
mypy 0.620+dev-f5058bcb269a55b33a02c3e3fd345541839bf06e
Flags I'm using:
strict_optional = true
ignore_missing_imports = true
follow_imports = silent
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
Riproduci l'esempio Python inline con le impostazioni segnalate strict_optional, ignore_missing_imports e follow_imports. Inizia tracciando l'inferenza di mypy per l'assegnazione dell'attributo Generic TypeVar; il lavoro è completato quando l'esempio non richiede più un'annotazione esplicita e il comportamento è coperto da un test di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100