`TypedMapping` for extended compatibility?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
Hello,
Consider the following, where lock aims at making provided argument immutable.
@dataclass
class Works:
foo: Mapping
def lock(self) -> None:
self.foo = MappingProxyType(self.foo)
It works great with mypy, no issue. Now imagine I want to enforce a bit more to foo by requesting it be compatible with a Movie. I would try the following, without success.
class Movie(TypedDict):
author: str
year: int
@dataclass
class Fails:
foo: Movie
def lock(self) -> None:
self.foo = MappingProxyType(self.foo) # error: Incompatible types in assignment (expression has type "MappingProxyType[str, object]", variable has type "Movie") [assignment]
Is there an workaround? If not, is if a good idea to discuss the possibility of a TypedMapping, that would simply work as follow?
class Movie(TypedMapping): # New feature ?
author: str
year: int
@dataclass
class Fixed:
foo: Movie
def lock(self) -> None:
self.foo = MappingProxyType(self.foo) # Would work
Thanks for your time.
All the best!
Élie.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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 mit den TypedDict-, Mapping- und MappingProxyType-Beispielen im Issue. Verfolge die gemeldete Inkompatibilität bei der Zuweisung und vergleiche das angeforderte TypedMapping-Verhalten mit den bestehenden Typing-Regeln; als erledigt gilt die Aufgabe, wenn entweder ein unterstützter Workaround dokumentiert oder ein klar abgegrenzter Feature-Vorschlag erstellt wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- developer-experience
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 28/100