Bad self type inference with Generics and inheritance?
Offen
Dieses Issue hat noch niemand übernommen.
bug
priority-2-low
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
- 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)
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem bereitgestellten Reproduzierer foo.py und führen Sie ihn mit den gemeldeten mypy-Versionen aus, um den falschen self-Typ für FooChild[T] zu bestätigen. Verfolgen Sie die generische Vererbung und den Konstruktoraufruf, die an mypys Typinferenz beteiligt sind, und fügen Sie Tests hinzu, die zeigen, dass der verschachtelte Fall Bar[T] ohne Fehler erfolgreich ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100