mypy not flagging subclasses with incompatible constructors
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
reproducer
from typing import Type
class Foo:
def __init__(self, a):
self.a = a
class Bar(Foo):
def __init__(self):
super().__init__("Bar")
def takes_foo(foo: Type[Foo]):
x = foo("Foo")
takes_foo(Foo)
takes_foo(Bar)
observed behavior
mypy raises no errors, but the code raises a TypeError at runtime.
expected behavior
pep 484 states:
when
new_user()callsuser_class()this implies that all subclasses ofUsermust support this in their constructor signature...A type checker ought to flag violations of such assumptions, but by default constructor calls that match the constructor signature in the indicated base class (User in the example above) should be allowed.
My understanding of the above is that mypy should be raising an error on the Bar definition because its signature is incompatible with that of its superclass. More generally, I tend to be surprised when code that passes mypy raises a TypeError at runtime.
environment
master as of 2019-06-10
$ pipenv graph
mypy==0.710+dev.e2f31ed71bd1edd60bffc86d3fda9da15ba63b3d
- mypy-extensions [required: >=0.4.0,<0.5.0, installed: 0.4.1]
- typed-ast [required: >=1.4.0,<1.5.0, installed: 1.4.0]
$ pipenv run python --version
Python 3.7.3
Thank you all for an amazing tool!
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 il riproduttore nell’issue e confronta il suo comportamento con le indicazioni di PEP 484 sui costruttori e sugli oggetti classe. Analizza la gestione da parte di mypy delle firme dei costruttori delle sottoclassi e determina se il risultato previsto sia un errore su Bar o su takes_foo(Bar). Il lavoro è completato quando il comportamento è coperto da un test di regressione e il checker segnala l’incompatibilità prevista.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100