Skip to main content

Python library to convert dataclasses into marshmallow schemas.

Project description

marshmallow-dataclass

Test Workflow Status (master branch) PyPI version marshmallow 3 compatible download stats

Automatic generation of marshmallow schemas from dataclasses.

from dataclasses import dataclass, field
from typing import List, Optional

import marshmallow_dataclass
import marshmallow.validate


@dataclass
class Building:
    # 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=list)


city_schema = marshmallow_dataclass.class_schema(City)()

city = city_schema.load(
    {"name": "Paris", "buildings": [{"name": "Eiffel Tower", "height": 324}]}
)
# => City(name='Paris', buildings=[Building(height=324.0, name='Eiffel Tower')])

city_dict = city_schema.dump(city)
# => {'name': 'Paris', 'buildings': [{'name': 'Eiffel Tower', 'height': 324.0}]}

Why

Using schemas in Python often means having both a class to represent your data and a class to represent its schema, which results in duplicated code that could fall out of sync. As of Python 3.6, types can be defined for class members, which allows libraries to generate schemas automatically.

Therefore, you can document your APIs in a way that allows you to statically check that the code matches the documentation.

Installation

This package is hosted on PyPI.

pip3 install marshmallow-dataclass
pip3 install "marshmallow-dataclass"

marshmallow 2 support

marshmallow-dataclass no longer supports marshmallow 2. Install marshmallow_dataclass<6.0 if you need marshmallow 2 compatibility.

Usage

Use the class_schema function to generate a marshmallow Schema class from a dataclass.

from dataclasses import dataclass
from datetime import date

import marshmallow_dataclass


@dataclass
class Person:
    name: str
    birth: date


PersonSchema = marshmallow_dataclass.class_schema(Person)

The type of your fields must be either basic types supported by marshmallow (such as float, str, bytes, datetime, ...), Union, or other dataclasses.

Union (de)serialization coercion

Typically the Union type; Union[X, Y] means—from a set theory perspective—either X or Y, i.e., an unordered set, howevever the order of the sub-types defines the precedence when attempting to ether deserialize or serialize the value per here.

For example,

from typing import Union

from dataclasses import dataclass


@dataclass
class Person:
    name: str
    age: Union[int, float]


PersonSchema = marshmallow_dataclass.class_schema(Person)
PersonSchema().load({"name": "jane", "age": 50.0})
# => Person(name="jane", age=50)

will first (sucessfully) try to coerce 50.0 to an int. If coercion is not desired the Any type can be used with the caveat that values will not be type checked without additional validation.

Customizing generated fields

To pass arguments to the generated marshmallow fields (e.g., validate, load_only, dump_only, etc.), pass them to the metadata argument of the field function.

Note that starting with version 4, marshmallow will disallow passing arbitrary arguments, so any additional metadata should itself be put in its own metadata dict:

from dataclasses import dataclass, field
import marshmallow_dataclass
import marshmallow.validate


@dataclass
class Person:
    name: str = field(
        metadata=dict(
            load_only=True, metadata=dict(description="The person's first name")
        )
    )
    height: float = field(metadata=dict(validate=marshmallow.validate.Range(min=0)))


PersonSchema = marshmallow_dataclass.class_schema(Person)

@dataclass shortcut

marshmallow_dataclass provides a @dataclass decorator that behaves like the standard library's @dataclasses.dataclass and adds a Schema attribute with the generated marshmallow Schema.

# Use marshmallow_dataclass's @dataclass shortcut
from marshmallow_dataclass import dataclass


@dataclass
class Point:
    x: float
    y: float


Point.Schema().dump(Point(4, 2))
# => {'x': 4, 'y': 2}

Note: Since the .Schema property is added dynamically, it can confuse type checkers. To avoid that, you can declare Schema as a ClassVar.

from typing import ClassVar, Type

from marshmallow_dataclass import dataclass
from marshmallow import Schema


@dataclass
class Point:
    x: float
    y: float
    Schema: ClassVar[Type[Schema]] = Schema

Customizing the base Schema

It is also possible to derive all schemas from your own base Schema class (see marshmallow's documentation about extending Schema). This allows you to implement custom (de)serialization behavior, for instance specifying a custom mapping between your classes and marshmallow fields, or renaming fields on serialization.

Custom mapping between classes and fields

class BaseSchema(marshmallow.Schema):
    TYPE_MAPPING = {CustomType: CustomField, List: CustomListField}


class Sample:
    my_custom: CustomType
    my_custom_list: List[int]


SampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=BaseSchema)
# SampleSchema now serializes my_custom using the CustomField marshmallow field
# and serializes my_custom_list using the CustomListField marshmallow field

Renaming fields on serialization

import marshmallow
import marshmallow_dataclass


class UppercaseSchema(marshmallow.Schema):
    """A Schema that marshals data with uppercased keys."""

    def on_bind_field(self, field_name, field_obj):
        field_obj.data_key = (field_obj.data_key or field_name).upper()


class Sample:
    my_text: str
    my_int: int


SampleSchema = marshmallow_dataclass.class_schema(Sample, base_schema=UppercaseSchema)

SampleSchema().dump(Sample(my_text="warm words", my_int=1))
# -> {"MY_TEXT": "warm words", "MY_INT": 1}

You can also pass base_schema to marshmallow_dataclass.dataclass.

@marshmallow_dataclass.dataclass(base_schema=UppercaseSchema)
class Sample:
    my_text: str
    my_int: int

See marshmallow's documentation about extending Schema.

Custom type aliases

This library allows you to specify customized marshmallow fields using python's Annoted type PEP-593.

from typing import Annotated
import marshmallow.fields as mf
import marshmallow.validate as mv

IPv4 = Annotated[str, mf.String(validate=mv.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$"))]

You can also pass a marshmallow field class.

from typing import Annotated
import marshmallow
from marshmallow_dataclass import NewType

Email = Annotated[str, marshmallow.fields.Email]

For convenience, some custom types are provided:

from marshmallow_dataclass.typing import Email, Url

When using Python 3.8, you must import Annotated from the typing_extensions package

# Version agnostic import code:
if sys.version_info >= (3, 9):
    from typing import Annotated
else:
    from typing_extensions import Annotated

Custom NewType declarations [deprecated]

NewType is deprecated in favor or type aliases using Annotated, as described above.

This library exports a NewType function to create types that generate customized marshmallow fields.

Keyword arguments to NewType are passed to the marshmallow field constructor.

import marshmallow.validate
from marshmallow_dataclass import NewType

IPv4 = NewType(
    "IPv4", str, validate=marshmallow.validate.Regexp(r"^([0-9]{1,3}\\.){3}[0-9]{1,3}$")
)

You can also pass a marshmallow field to NewType.

import marshmallow
from marshmallow_dataclass import NewType

Email = NewType("Email", str, field=marshmallow.fields.Email)

Note: if you are using mypy, you will notice that mypy throws an error if a variable defined with NewType is used in a type annotation. To resolve this, add the marshmallow_dataclass.mypy plugin to your mypy configuration, e.g.:

[mypy]
plugins = marshmallow_dataclass.mypy
# ...

Meta options

Meta options are set the same way as a marshmallow Schema.

from marshmallow_dataclass import dataclass


@dataclass
class Point:
    x: float
    y: float

    class Meta:
        ordered = True

Documentation

The project documentation is hosted on GitHub Pages: https://lovasoa.github.io/marshmallow_dataclass/

Contributing

To install this project and make changes to it locally, follow the instructions in CONTRIBUTING.md.

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

marshmallow_dataclass-8.7.1.tar.gz (32.1 kB view details)

Uploaded Source

Built Distribution

marshmallow_dataclass-8.7.1-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_dataclass-8.7.1.tar.gz.

File metadata

  • Download URL: marshmallow_dataclass-8.7.1.tar.gz
  • Upload date:
  • Size: 32.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for marshmallow_dataclass-8.7.1.tar.gz
Algorithm Hash digest
SHA256 4fb80e1bf7b31ce1b192aa87ffadee2cedb3f6f37bb0042f8500b07e6fad59c4
MD5 f158473050f0e91ccb87be8090e7f439
BLAKE2b-256 0123a863a5d569f03454d733f884a72415ac3f1e1b1b3215de3a9f4f621a83a6

See more details on using hashes here.

File details

Details for the file marshmallow_dataclass-8.7.1-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_dataclass-8.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 405cbaaad9cea56b3de2f85eff32a9880e3bf849f652e7f6de7395e4b1ddc072
MD5 18afa089c89984450eb1a28cd45045ce
BLAKE2b-256 3ef56764f3f3203d14a0e6df0fce4838f8195ccc61ec7d48d7ed89acfb8adeed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page