Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

marshmallow_dataclass

Build Status

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 --pre marshmallow-dataclass

This package has the following optional features:

You can install them with:

pipenv install marshmallow-dataclass[enum]

Documentation

The project documentation is hosted on github pages:

Usage warning

This library depends on python's standard typing library, which is provisional.

Release files for marshmallow-dataclass 6.0.0rc3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marshmallow-dataclass 6.0.0rc3
File Size Uploaded
marshmallow_dataclass-6.0.0rc3.tar.gz 7.0 kB Details

Release files / marshmallow_dataclass-6.0.0rc3.tar.gz

Download URL marshmallow_dataclass-6.0.0rc3.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b8f8ce9d6d46efb40e9e2ee0fc65213a3ddb9bc6e88be4b3824d38c692d39a8c
BLAKE2b-256 checksum
How to use checksums
3c0b6433cc15078f5b3e2f6988663877794851660ab33861bf9008b605c95c90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.1

Release history Release notifications | RSS feed

8.7.1

2 release files

8.7.0

2 release files

8.6.1

2 release files

8.6.0

2 release files

8.5.14

2 release files

8.5.13

2 release files

8.5.12

2 release files

8.5.9

2 release files

8.5.8

2 release files

8.5.7

2 release files

8.5.6

2 release files

8.5.5

2 release files

8.5.4

2 release files

8.5.3

2 release files

8.5.2

2 release files

8.5.1

2 release files

8.5.0

2 release files

8.4.2

2 release files

8.4.1

2 release files

8.4.0

2 release files

8.3.2

2 release files

8.3.1

2 release files

8.3.0

2 release files

8.2.0

2 release files

8.1.0

1 release file

8.0.0

1 release file

7.6.0

1 release file

7.5.2

1 release file

7.5.1

1 release file

7.5.0

1 release file

7.4.0

1 release file

7.3.0

1 release file

7.2.1

1 release file

7.2.0

1 release file

7.1.1

1 release file

7.1.0

1 release file

7.0.0

1 release file

6.1.0

1 release file

6.0.0

1 release file

This release

6.0.0rc3 This release

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page