Python library to convert dataclasses into marshmallow schemas.
Project description
marshmallow_dataclass
Automatic generation of marshmallow schemas from dataclasses.
Specifying a schema to which your data should conform is very useful, both for (de)serialization and for documentation. However, using schemas in python often means having both a class to represent your data and a class to represent its schema, which means duplicated code that could fall out of sync. With the new features of python 3.6, types can be defined for class members, and that allows libraries like this one to generate schemas automatically.
An use case would be to document APIs (with flasgger, for instance) in a way that allows you to statically check that the code matches the documentation.
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
import marshmallow.validate
from typing import List, Optional
@dataclass
class Building:
# The field metadata is used to instantiate the marshmallow field
height: float = field(metadata={'validate': marshmallow.validate.Range(min=0)})
name: str = field(default="anonymous")
@dataclass
class City:
name: Optional[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}
]
})
# Serializing city as a json string
city_json = City.Schema().dumps(city)
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
You can specify the
Meta
just as you would in a marshmallow Schema:
from marshmallow_dataclass import dataclass
@dataclass
class Point:
x:float
y:float
class Meta:
ordered = True
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
File details
Details for the file marshmallow_dataclass-6.0.0b8.tar.gz
.
File metadata
- Download URL: marshmallow_dataclass-6.0.0b8.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 043c79d774de519897c67fe064f597655e96426f064023204c403bfed859ad31 |
|
MD5 | ea74430d96cd1ea29f5c061f1a3b9b6a |
|
BLAKE2b-256 | 8e6164275013e3cf3599525ec1f7a31ba76506c14e2c07890bd5a094933e75a1 |