Generic class with constrained type vars
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
On Python 3.7.0 and mypy 0.620, I'm getting some error messages I don't understand. The Mypy documentation and PEP 484 didn't clear it up for me. Maybe it's a bug in Mypy. Maybe I don't understand type erasure. In case it's the former, here's a minimal example. In case it's the latter, please help.
In the following examples, the fact that m does not return is not critical. In the real code that gave me these errors, m is an abstract method, which also doesn't change anything important.
Put the following examples in mypy-test.py to get the corresponding output from running mypyt mypy-test.py. The first and the last examples are the ones that are baffling me.
classmethod with constrained type variable
from typing import TypeVar, Generic
T = TypeVar('T', int, str)
class K(Generic[T]):
@classmethod
def m(cls) -> T:
raise NotImplementedError
Mypy output:
mypy-test.py:5: error: The erased type of self 'def [T in (builtins.int, builtins.str)] () -> mypy-test.K[builtins.int*]' is not a supertype of its class 'Type[mypy-test.K[T`1]]'
mypy-test.py:5: error: The erased type of self 'def [T in (builtins.int, builtins.str)] () -> mypy-test.K[builtins.str*]' is not a supertype of its class 'Type[mypy-test.K[T`1]]'
classmethod with generic self
from typing import TypeVar, Generic
S = TypeVar('S')
T = TypeVar('T', int, str)
class K(Generic[T]):
@classmethod
def m(cls: S) -> T:
raise NotImplementedError
produces no error.
classmethod with unconstrained type variable
from typing import TypeVar, Generic
T = TypeVar('T')
class K(Generic[T]):
@classmethod
def m(cls) -> T:
raise NotImplementedError
produces no error.
instance method with constrained type
from typing import TypeVar, Generic
T = TypeVar('T', int, str)
class K(Generic[T]):
def __init__(self, t: T) -> None:
self.t = t
produces
mypy-test.py:5: error: Need type annotation for 't'
instance method with unconstrained type
from typing import TypeVar, Generic
T = TypeVar('T')
class K(Generic[T]):
def __init__(self, t: T) -> None:
self.t = t
produces no error.
instance method with constrained type and inheritance
from typing import TypeVar, Generic
T = TypeVar('T', int, str)
class K(Generic[T]):
def __init__(self, t: T) -> None:
self.t: T = t
class L(K[T]):
def __init__(self, t: T) -> None:
self.u = 1
super().__init__(t=t)
produces
mypy-test.py:10: error: Argument "t" to "__init__" of "K" has incompatible type "int"; expected "T"
mypy-test.py:10: error: Argument "t" to "__init__" of "K" has incompatible type "str"; expected "T"
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 gli esempi in mypy-test.py con Python 3.7.0 e mypy 0.620, iniziando dai metodi di classe generici vincolati e non vincolati. Confronta le diagnosi per i metodi di classe, gli attributi di istanza e i costruttori ereditati, quindi determina il comportamento previsto prima di decidere se sono necessarie modifiche all’implementazione o test di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100