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
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

marshmallow_dataclass-6.0.0b2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marshmallow_dataclass-6.0.0b2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_dataclass-6.0.0b2.tar.gz.

File metadata

  • Download URL: marshmallow_dataclass-6.0.0b2.tar.gz
  • Upload date:
  • Size: 5.8 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-6.0.0b2.tar.gz
Algorithm Hash digest
SHA256 2c04e6f6c83654eb6e3c8189fa274b262c554d0a4e2d0d51fbd4e71b84b4259c
MD5 e4809e5061a59e35302563920e3a444c
BLAKE2b-256 653df1f163d1126769da6bbdf2fc769a8b15789ea9cf6cb5b9b05974b2db0008

See more details on using hashes here.

File details

Details for the file marshmallow_dataclass-6.0.0b2-py3-none-any.whl.

File metadata

  • Download URL: marshmallow_dataclass-6.0.0b2-py3-none-any.whl
  • Upload date:
  • Size: 7.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-6.0.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b8d865f55d7419eea7621df6f8cc0117675e80e47ef48fb71941d0d9ca6f9a6
MD5 490d3bc55cf5916be54e116223efa4c1
BLAKE2b-256 469f24ce999a3e45caf0619f0173ff29fcad0f12d67fa0cb00b73adba490979b

See more details on using hashes here.

Supported by

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