Protocols for types with fields that can be verified
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beschreibung
$ pip freeze | rg mypy
mypy-extensions==0.4.1
mypy-mypyc==0.670
I have some complex objects that can be in one of several states once they have been verified, and I'd like to represent that verification as different types, but I'm having a hard time getting it to work with protocols.
I believe that something like this should work:
from __future__ import annotations
from typing import NamedTuple, Protocol, Optional
class Simple:
two: Optional[int]
def as_simpl_verified(self) -> SimpleVerified:
assert self.two is not None
return self
class SimpleVerified(Protocol):
two: int
but I get this error:
$ mypy example_simple.py
example_simple.py:11: error: Incompatible return value type (got "Simple", expected "SimpleVerified")
example_simple.py:11: note: Following member(s) of "Simple" have conflicts:
example_simple.py:11: note: two: expected "int", got "Optional[int]
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
Reproduziere den Bericht in example_simple.py mit dem gezeigten mypy-Befehl und untersuche anschließend, wie Protokolle verifizierte Feldtypen behandeln. Kläre das vorgesehene akzeptierte Verhalten und füge dafür Abdeckung hinzu; fertig ist die Aufgabe, wenn das Beispiel das vereinbarte Ergebnis statt der gemeldeten Diagnose zu inkompatiblen Rückgabewerten erzeugt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100