python / python/mypy

Code review request: support for asdict returning TypedDict

Offen
#10,104 4 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-dataclasses
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

I'm sorry for abusing the issue tracker, but I'm not sure how else to notify someone who could perform a code-review (I did try pinging/mentions). And no worries of course :)

I have brought this PR up-to-date and passing tests again and would like to request a re-review: https://github.com/python/mypy/pull/8583/

@JukkaL Perhaps you have the time at some point to take a look or could ping someone who does?

Although the feature is already mentioned in #5152, I'll try to pitch it.

The pitch:

I have a dataclass that happens to have the same (or a subset of the same) fields as another class.
The list is potentially long, and each one has a different type.
I would like to construct the class out of the dataclass, without having to explicitly list each field as an argument to the class's constructor. It's easy to do this at run-time, but I would like to ensure that it is also statically checked.

To avoid listing each field manually, I can take advantage of splatting the TypedDict.
This should allow mypy to type-check that I do in fact pass the correct arguments to the class constructor.

@dataclass
class FooEntity:
  bar: str
  # long list of fields...

class Foo:
  def __init__(bar: str, ...): pass

def create_foo_from_entity(foo: FooEntity) -> Foo:
  return Foo(**asdict(foo))

I could think of a similar example with a function instead of a class constructor.

You could also have a case where a function is expected to return a certain TypedDict (let's say it's part of an existing API), and you have decided to use dataclasses as your internal representation, among other reasons, because they can have methods, are mutable, and possible to build-up incrementally. To avoid having to explicitly construct the TypedDict from the dataclass (which may include nested dataclasses), you could just call asdict on it and still be assured that the types match what is expected.

I can't otherwise see many use cases for this, as I would assume that many people just directly serialize the return value of asdict, and so don't use it in a precisely-typed manner.

Finally, as a future idea, it would be interesting to be able to use a type annotation AsDict[MyDataclass] to represent the TypedDict returned by calling asdict(instance_of_my_dataclass). This would allow the information about which TypedDict was returned to flow across function boundaries.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Prüfung von pull request 8583, der den vorgeschlagenen Support enthält, und vergleiche ihn mit der Feature-Diskussion in issue 5152. Die unmittelbare Anfrage des Issues ist ein Code-Review und keine noch nicht begonnene Implementierung; für den Abschluss müssten das Review geklärt und festgestellt werden, ob das vorgeschlagene asdict-to-TypedDict-Verhalten akzeptiert werden sollte.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
15/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.