Skip to main content

Bake marshmallow schemas based on dataclasses

Project description

marshmallow-recipe

The main goal of this opinionated library is to simplify migration from marshmallow2 to marshmallow3. Also, it helps with:

  1. Stop writing marshmallow schemas completely: it generates them from dataclass.
  2. Using different naming cases(camel and capital camel cases are supported).
  3. Utilizing best practises on fields configuration.
import dataclasses
import datetime
import decimal
import marshmallow_recipe as mr
import uuid

@dataclasses.dataclass(frozen=True)
class Transaction:
    id: uuid.UUID
    created_at: datetime.datetime
    processed_at: datetime.datetime | None
    amount: decimal.Decimal

transaction = Transaction(
    id=uuid.uuid4(),
    created_at=datetime.datetime.utcnow(),
    processed_at=None,
    amount=decimal.Decimal(42),
 )

# dumps the transaction to a dict
raw = mr.dump(transaction) 

# loads a transaction from the dict
mr.load(Transaction, raw)

# provides a generated marshmallow schema for dataclass
mr.schema(Transaction)

Update API example:

import decimal
import dataclasses
import marshmallow_recipe as mr

@dataclasses.dataclass(frozen=True)
@mr.options(none_value_handling=mr.NoneValueHandling.INCLUDE)
class CompanyUpdateData:
    name: str = mr.MISSING
    annual_turnover: decimal.Decimal | None = mr.MISSING

company_update_data = CompanyUpdateData(name="updated name")
dumped = mr.dump(company_update_data)
assert dumped == {"name": "updated name"}  # Note: no "annual_turnover" here

loaded = mr.load(CompanyUpdateData, {"name": "updated name"})
assert loaded.name == "updated name"
assert loaded.annual_turnover is mr.MISSING

loaded = mr.load(CompanyUpdateData, {"annual_turnover": None})
assert loaded.name is mr.MISSING
assert loaded.annual_turnover is None

v0.0.15(2022-12-01)

v0.0.14(2022-11-14)

v0.0.13(2022-10-14)

v0.0.12(2022-08-23)

v0.0.11(2022-06-23)

v0.0.10(2022-06-15)

v0.0.9(2022-04-26)

v0.0.8(2022-03-26)

v0.0.7(2022-03-20)

v0.0.6(2022-03-20)

v0.0.5(2022-03-01)

v0.0.4(2022-02-21)

v0.0.3 (2022-02-14)

v0.0.2 (2022-02-13)

v0.0.1 (2022-02-13)

  • A first version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marshmallow-recipe-0.0.15.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

marshmallow_recipe-0.0.15-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow-recipe-0.0.15.tar.gz.

File metadata

  • Download URL: marshmallow-recipe-0.0.15.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for marshmallow-recipe-0.0.15.tar.gz
Algorithm Hash digest
SHA256 aff324f182473ded20ad2315dd5270926b4511e36ef9244441c97ad8cd20dc1e
MD5 53ebc23b16b7c56360a0300bf043b65b
BLAKE2b-256 6fc5b767364809be0f184c8b4bde344f71939f030481ed3c289fa3fa10cf3197

See more details on using hashes here.

File details

Details for the file marshmallow_recipe-0.0.15-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_recipe-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 db6fab5cb92b761f617ab4a69128326026b7f6525fccb7749eda8a1d5d5cef8e
MD5 e3076643c68e6e9e688bb6da469001b4
BLAKE2b-256 46dbdca504ea1b80fd05a52ab0f3f06cb24f2ebf4bb3c1965fcdf2e61bacf505

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