Skip to main content

Python library to convert dataclasses into marshmallow schemas.

Project description

marshmallow_dataclass

Build Status

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

This version

0.2.0

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-0.2.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

marshmallow_dataclass-0.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: marshmallow_dataclass-0.2.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

Hashes for marshmallow_dataclass-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f43eb21ba4d8f82a249c04a7c5857d059f1e6f2921008ad45b909e0f364dddcb
MD5 99cf62ed91c3319aa351f9d6e6d06fcc
BLAKE2b-256 2a37f5541fd5c5b17afc7b3f971dd8130e0e9b5d8b8d7428bb8fad0afa620c78

See more details on using hashes here.

File details

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

File metadata

  • Download URL: marshmallow_dataclass-0.2.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

Hashes for marshmallow_dataclass-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 375646e1ff0fdd7ab8a154b5636e9354e8228104dcc372afe7b7703fa447c98b
MD5 aa9ddf62b4c41a1f4687c6aface0faa3
BLAKE2b-256 da9fb87098f8f7d260f09b0e56e4ab96505954a340bdae7267dfc225e69d1d7f

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