Python library to convert dataclasses into marshmallow schemas.
Project description
marshmallow_dataclass
Automatic generation of marshmallow schemas from dataclasses.
How to use
You simply import
marshmallow_dataclass.dataclass
instead of
dataclasses.dataclass
.
It adds a Schema
property to the generated class,
containing a marshmallow
Schema
class.
If you need to specify custom properties on your marshmallow fields
(such as attribute
, error
, validate
, required
, dump_only
, error_messages
, description
...)
you can add them using the metadata
argument of the
field
function.
from dataclasses import field
from marshmallow_dataclass import dataclass # Importing from marshmallow_dataclass instead of dataclasses
from typing import List
@dataclass
class Building:
# The field metadata is used to instantiate the marshmallow field
height: float = field(metadata={'required':True})
name: str = field(default="anonymous")
@dataclass
class City:
name: str
buildings: List[Building] = field(default_factory=lambda: [])
# City.Schema contains a marshmallow schema class
city, _ = City.Schema().load({
"name": "Paris",
"buildings": [
{"name": "Eiffel Tower", "height":324}
]
})
The previous syntax is very convenient, as the only change
you have to apply to your existing code is update the
dataclass
import.
However, as the .Schema
property is added dynamically,
it can confuse type checkers.
If you want to avoid that, you can also use the standard
dataclass
decorator, and generate the schema manually
using
class_schema
:
from dataclasses import dataclass
from datetime import datetime
import marshmallow_dataclass
@dataclass
class Person:
name: str
birth: datetime
PersonSchema = marshmallow_dataclass.class_schema(Person)
You can also declare the schema as a
ClassVar
:
from marshmallow_dataclass import dataclass
from marshmallow import Schema
from typing import ClassVar, Type
@dataclass
class Point:
x:float
y:float
Schema: ClassVar[Type[Schema]] = Schema
installation
This package is hosted on pypi :
pipenv install marshmallow-dataclass
Documentation
The project documentation is hosted on github pages:
Usage warning
This library depends on python's standard typing library, which is provisional.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file marshmallow_dataclass-0.3.0.tar.gz
.
File metadata
- Download URL: marshmallow_dataclass-0.3.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.7.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c401c41826af9871b1e10b7ea11f21ab7609a60d73cd9029c77169b3c3d4077 |
|
MD5 | cce975cfe54326d4176639a8bad65e81 |
|
BLAKE2b-256 | ee07e1372e74b70852fd8902793053c79f803e0a7a3283272a16264082e5312e |
File details
Details for the file marshmallow_dataclass-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_dataclass-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.7.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b73caf8f3cb9d08b9d332a8016ac122b3e3d09a4b3fb3085784958bd55f3b30a |
|
MD5 | 69bb69b85ca7ed98feae37d680a9da54 |
|
BLAKE2b-256 | 843d78bf436fd41f2dae527919388aa25008a9a1bc8a33bbd0c606800c0f1936 |