question-mark
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.

Assign to __schema__ instead of Schema; load and dump helpers?

See original GitHub issue

It 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Sep 19, 2019

Yep, sounds good–documenting class_schema as preferred reduces the effective API surface. Will work on a docs PR when I have time.

0reactions
lovasoacommented, Sep 19, 2019

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 about class_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 is class_schema, and that we also provide a few niceties like @add_schema and @dataclass.

Read more comments on GitHub >

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

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found