False negative for restricted self in subclass
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
This currently passes, but the code is unsafe:
from typing import TypeVar, Generic
T = TypeVar('T')
class Base(Generic[T]):
def __init__(self, item: T) -> None:
self.item = item
def foo(self) -> None:
pass
class Sub(Base[T]):
def foo(self: Sub[str]) -> None:
self.item + 'no'
# This is why the above is unsafe.
a: Base[int] = Sub(0)
a.foo()
On one hand it is a very rare situation, on the other hand in view of making explicit self-types a more "official" feature (see https://github.com/python/mypy/pull/7860) we might want to fix this.
Also it would be great to fix this in a consistent way, so that the fix also works for same issue with multiple inheritance without code duplication (see https://github.com/python/mypy/issues/7724).
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
Beginne damit, das Beispiel im Issue zu reproduzieren, lies dann die explizite self-type-Arbeit in PR 7860 und den Fall der Mehrfachvererbung in Issue 7724. Der Fix sollte diese unsichere Verwendung von Subklassen konsistent zurückweisen und das entsprechende Verhalten bei Mehrfachvererbung ohne doppelte Logik abdecken.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100