Assign to __schema__ instead of Schema; load and dump helpers?
See original GitHub issueIt may make sense to assign the generated schema to __schema__
instead of Schema
for consistency with dataclasses.dataclass
, which only assigns dunder methods.
@dataclass
class Artist:
name: str
Artist.__schema__ # => Schema class
To improve the DX, you could add dump
and load
helper methods.
import datetime as dt
from marshmallow_dataclass import dataclass, load
@dataclass
class Album:
title: str
release_date: dt.date
album = load(Album, {"title": "Hunky Dory", "release_date": "1971-12-17"})
Schema contructor arguments can be passed as keyword arguments.
albums = load(Album, input_data, many=True)
This could even open the door to conveniences like
albums = load(List[Album], input_data) # calls Album.__schema__(many=True).load(input_data)
What do you think?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Extending Schemas — marshmallow 3.19.0 documentation
By default, pre- and post-processing methods receive one object/datum at a time, transparently handling the many parameter passed to the Schema 's dump()...
Read more >rake db:schema:dump and rake db:schema:load equivalent in ...
Loading a migration dumped by the schema dumper should only be done on an empty database.
Read more >how is the "only" attribute of a schema intended to be used ...
This gives me the impression that it is a separate attribute from fields that is used on serialization to know which fields to...
Read more >A Walkthrough of SQL Schema - SQLShack
Change SQL schema of an existing object in SQL Server · Right-click on the specific table name and choose Design option: · It...
Read more >Active Record Migrations - Rails Edge Guides
Rather than write schema modifications in pure SQL, migrations allow you to use a ... When Helpers aren't Enough; Using the change Method;...
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
Yep, sounds good–documenting
class_schema
as preferred reduces the effective API surface. Will work on a docs PR when I have time.I don’t think there is really a need to deprecate
@dataclass
. It is a small function, and it will not be a pain to maintain. We can just make the change you suggested in the README, making the first example aboutclass_schema
, and talking about@dataclass
only below. I developed this library for my own needs initially, and it was all about reducing boilerplate. I found that@dataclass
helped me maintain short, clean, and well-organized model definitions. I can completely understand why one wouldn’t like it, though. Let’s just make it clear in the README that the central part of this library isclass_schema
, and that we also provide a few niceties like@add_schema
and@dataclass
.