Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate a TypedDict or dataclass from Schema

See original GitHub issue

It would be nice to be able to use Schemata 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 is annotated automatically as : str.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

sloriacommented, Sep 17, 2019

I’ve been considering something like this. It’s a neat idea.

A few half-baked thoughts:

  • You would need to generate types from schema instances rather than classes. Conceptually, a schema instance represents a set of known inputs/outputs, whereas a schema class is more like a blueprint or factory.
  • There need to be different TypedDicts generated for load vs dump. So you could have an API like
def user_detail(user: LoadType(UserSchema())) -> DumpType(UserSchema()):
    # ...
  • This is pretty tricky given that post_load and post_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.

I do think this would be very nice to have though, or perhaps even go further and have a @deser_to_dataclass

You might want to look into . It generates a schema from dataclass that also deserializes to the same dataclass.

killthekittencommented, Sep 21, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found