Bad self type inference with Generics and inheritance?
Aperta
Nessuno ha ancora preso questa issue.
bug
priority-2-low
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
- Are you reporting a bug, or opening a feature request?
Reporting a bug
- Please insert below the code you are checking with mypy
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[Bar[T]]):
def __init__(self, blah: T):
Foo.__init__(self)
- What is the actual behavior/output?
$ mypy foo.py
foo.py:18: error: Argument 1 to "__init__" has incompatible type "FooChild[T]"; expected "Foo[T]"
Found 1 error in 1 file (checked 1 source file)
- What is the behavior/output you expect?
Success: no issues found in 1 source file
- What are the versions of mypy and Python you are using?
mypy 0.750
Python 3.7.3
- Do you see the same issue after installing mypy from Git master?
Yes, successfully reproduced with mypy 0.790+dev.df6894b5a9bb1a913e66df87138cd7a240c09fc1
- What are the mypy flags you are using? (For example --strict-optional)
No flags.
- If mypy crashed with a traceback, please paste
the full traceback below.
No crash
- Additional information:
The following versions typecheck successfully:
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[T]): # Removed one level of nesting here
def __init__(self, blah: T):
Foo.__init__(self)
from typing import TypeVar, Generic
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self): ...
class Bar(Generic[T]): ...
class FooChild(Foo[Bar[T]]):
def __init__(self): # Removed the argument here
Foo.__init__(self)
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 foo.py fornito ed eseguilo con le versioni di mypy indicate, confermando il tipo self errato per FooChild[T]. Traccia l’ereditarietà generica e la chiamata al costruttore coinvolte nell’inferenza dei tipi di mypy, quindi aggiungi una copertura che dimostri che il caso annidato Bar[T] ha esito positivo senza errori.
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