Skip to main content

marshmallow-enum

Enum field for use with Marshmallow.

Installation

pip install --user marshmallow_enum

If you're on a version before 3.4, you'll also need to install enum34.

Using The Field

To make use of the field, you must have an existing Enum:

from enum import Enum


class StopLight(Enum):
    green = 1
    yellow = 2
    red = 3

Then, declare it as a field in a schema:

from marshmallow import Schema
from marshmallow_enum import EnumField


class TrafficStop(Schema):
    light_color = EnumField(StopLight)

By default, the field will load and dump based on the name given to an enum value.

schema = TrafficStop()
schema.dump({'light_color': EnumField.red}).data
# {'light_color': 'red'}

schema.load({'light_color': 'red'}).data
# {'light_color': StopLight.red}

Customizing loading and dumping behavior

To customize how an enum is serialized or deserialized, there are three options:

  • Setting by_value=True. This will cause both dumping and loading to use the value of the enum.
  • Setting load_by=EnumField.VALUE. This will cause loading to use the value of the enum.
  • Setting dump_by=EnumField.VALUE. This will cause dumping to use the value of the enum.

If either load_by or dump_by are unset, they will follow from by_value.

Additionally, there is EnumField.NAME to be explicit about the load and dump behavior, this is the same as leaving both by_value and either load_by and/or dump_by unset.

Custom Error Message

A custom error message can be provided via the error keyword argument. It can accept three format values:

  • {input}: The value provided to the schema field
  • {names}: The names of the individual enum members
  • {values}: The values of the individual enum members

Previously, the following inputs were also available but are deprecated and will be removed in 1.6:

  • {name}
  • {value}
  • {choices}

Version 1.5

Allow unicode enum values (sergeyzsg) Add support for Marshmallow 3 (svidela)

Version 1.4

  • Deprecate the {choices}, {name} and {value} format keys for custom error messages
  • Allow configure loading and dumping independently

Download files

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

Source Distribution

marshmallow-enum-1.5.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

marshmallow_enum-1.5.1-py2.py3-none-any.whl (4.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file marshmallow-enum-1.5.1.tar.gz.

File metadata

  • Download URL: marshmallow-enum-1.5.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.0

File hashes

Hashes for marshmallow-enum-1.5.1.tar.gz
Algorithm Hash digest
SHA256 38e697e11f45a8e64b4a1e664000897c659b60aa57bfa18d44e226a9920b6e58
MD5 7f4624f5b553c50895c33b5195108c10
BLAKE2b-256 8e8cceecdce57dfd37913143087fffd15f38562a94f0d22823e3c66eac0dca31

See more details on using hashes here.

File details

Details for the file marshmallow_enum-1.5.1-py2.py3-none-any.whl.

File metadata

  • Download URL: marshmallow_enum-1.5.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.7.0

File hashes

Hashes for marshmallow_enum-1.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 57161ab3dbfde4f57adeb12090f39592e992b9c86d206d02f6bd03ebec60f072
MD5 8d7f93cbd674015b878c199b82b96585
BLAKE2b-256 c659ef3a3dc499be447098d4a89399beb869f813fee1b5a57d5d79dee2c1bf51

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.5.1 This release

2 files

1.4.1

2 files

1.4

2 files

1.3

2 files

1.2

2 files

1.0

1 file

Supported by

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