Generate a TypedDict or dataclass from Schema
See original GitHub issueIt would be nice to be able to use Schema
ta with mypy or similar, e.g.
class User(Schema):
name = fields.String()
age = fields.Integer()
def have_a_birthday(user: User.type) -> User.type:
user['age'] += '1' # will fail type check
return user # typechecks OK
I started an inline implementation with Schema._declared_fields
, but since there’s nothing like a Field.deser_type
it was going to be quite a hacky map by cases - isinstance(cls, fields.String)
etc. - and then I realised I had nested fields to contend with and jumped out of the rabbit hole before I got too deep.
I do think this would be very nice to have though, or perhaps even go further and have a @deser_to_dataclass
decorator (that’s the route I was going) that re-writes the Schema
so that the type is User
(or whatever) itself, and User.name
is annotated automatically as : str
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Generate a TypedDict or dataclass from Schema - - Bountysource
A lightweight library for converting complex objects to and from simple Python datatypes. See More. Top Supporters. This team needs your support!
Read more >TypedDict vs dataclasses in Python — Epic typing BATTLE!
TypedDict doesn't work with isinstance - and for good reason. Under the hood, isinistance looks up the class name of the Python object....
Read more >GraphQL schema to python dataclasses codegen
gql_schema_codegen nice, but generating TypedDict which isn't dataclasses, I have to change each dict and pass total=False so it won't ...
Read more >Schema — marshmallow 3.19.0 documentation - Read the Docs
import datetime as dt from dataclasses import dataclass from marshmallow import Schema, ... Generate a Schema class given a dictionary of fields.
Read more >TypedDict, Dataclasses and Pydantic in action - YouTube
Peter Vidos - ipyvizzu - aopen-source charting tool to create animated data stories in Jupyter. PyCon Lithuania. PyCon Lithuania.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve been considering something like this. It’s a neat idea.
A few half-baked thoughts:
TypedDict
s generated for load vs dump. So you could have an API likepost_load
andpost_dump
methods can return any type.I probably won’t pursue this myself for a while, and I don’t think we should put this into marshmallow core at least until PEP 589 lands. If you end up experimenting with this in a 3rd-party repo, though, please let us know.
You might want to look into https://github.com/lovasoa/marshmallow_dataclass . It generates a schema from dataclass that also deserializes to the same dataclass.
How about building a schema based on an existing TypedDict class? There’s a plenty of information that can be inferred from the type definition, similar to
marshmallow-dataclass
.