Protocol Subclass checking method mentioned in documentation does not work / gives desired output
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Documentation
The current documentation https://mypy.readthedocs.io/en/latest/protocols.html#defining-subprotocols-and-subclassing-protocols mentions the following:
explicitly assigning to a protocol instance can be a way to ask the type checker to verify that your class implements a protocol:
_proto: SomeProto = cast(ExplicitSubclass, None)
Minimal Test
# test.py
from typing import Protocol, cast
class SomeProto(Protocol):
def method(self) -> str: ...
class ExplicitSubclass(SomeProto):
pass
_: SomeProto = cast(ExplicitSubclass, None)
Expected Behaviour
➤ mypy --strict --extra-checks test.py
test.py:12: ERROR ...
Actual Behaviour
➤ mypy --version
mypy 1.12.0+dev.5dfc7d941253553ab77836e9845cb8fdfb9d23a9 (compiled: no)
➤ mypy --strict --extra-checks test.py
Success: no issues found in 1 source file
Suggestion
Change the documentation example code line to:
_: type[SomeProto] = ExplicitSubclass
This actually triggers a type check of the subclass:
➤ mypy --strict --extra-checks testmypy.py
testmypy.py:12: error: Can only assign concrete classes to a variable of type "type[SomeProto]" [type-abstract]
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 Abschnitt zur Protocol-Subklassifizierung unter der verlinkten Dokumentations-URL und reproduzieren Sie den minimalen Test mit mypy --strict --extra-checks. Aktualisieren Sie das Beispiel so, dass es das im Issue gezeigte Verhalten widerspiegelt, und überprüfen Sie anschließend, dass das Beispiel in der Dokumentation die beabsichtigte Diagnose des Type-Checkers erzeugt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 1/5
- Geschätzter Aufwand
- Unter einer Stunde
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 48/100