An extension to marshmallow to support schema (de)multiplexing.
marshmallow is a fantastic library for serialization and deserialization of data. For more on that project see its GitHub page or its Documentation.
This library adds a special kind of schema that actually multiplexes other schemas based on object type. When serializing values, it uses get_obj_type() method to get object type name. Then it uses type_schemas name-to-Schema mapping to get schema for that particular object type, serializes object using that schema and adds an extra field with name of object type. Deserialization is reverse.
Installing
$ pip install marshmallow-oneofschema
Example
The code below demonstrates how to set up a polymorphic schema. For the full context check out the tests. Once setup the schema should act like any other schema. If it does not then please file an Issue.
import marshmallow
import marshmallow.fields
from marshmallow_oneofschema import OneOfSchema
class Foo:
def __init__(self, foo):
self.foo = foo
class Bar:
def __init__(self, bar):
self.bar = bar
class FooSchema(marshmallow.Schema):
foo = marshmallow.fields.String(required=True)
@marshmallow.post_load
def make_foo(self, data, **kwargs):
return Foo(**data)
class BarSchema(marshmallow.Schema):
bar = marshmallow.fields.Integer(required=True)
@marshmallow.post_load
def make_bar(self, data, **kwargs):
return Bar(**data)
class MyUberSchema(OneOfSchema):
type_schemas = {"foo": FooSchema, "bar": BarSchema}
def get_obj_type(self, obj):
if isinstance(obj, Foo):
return "foo"
elif isinstance(obj, Bar):
return "bar"
else:
raise Exception("Unknown object type: {}".format(obj.__class__.__name__))
MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
# => [{'type': 'foo', 'foo': 'hello'}, {'type': 'bar', 'bar': 123}]
MyUberSchema().load(
[{"type": "foo", "foo": "hello"}, {"type": "bar", "bar": 123}], many=True
)
# => [Foo('hello'), Bar(123)]
By default get_obj_type() returns obj.__class__.__name__, so you can just reuse that to save some typing:
class MyUberSchema(OneOfSchema):
type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
You can customize type field with type_field class property:
class MyUberSchema(OneOfSchema):
type_field = "object_type"
type_schemas = {"Foo": FooSchema, "Bar": BarSchema}
MyUberSchema().dump([Foo(foo="hello"), Bar(bar=123)], many=True)
# => [{'object_type': 'Foo', 'foo': 'hello'}, {'object_type': 'Bar', 'bar': 123}]
You can use resulting schema everywhere marshmallow.Schema can be used, e.g.
import marshmallow as m
import marshmallow.fields as f
class MyOtherSchema(m.Schema):
items = f.List(f.Nested(MyUberSchema))
License
MIT licensed. See the bundled LICENSE file for more details.
Release files for marshmallow-oneofschema 3.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| marshmallow_oneofschema-3.2.0.tar.gz | 9.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| marshmallow_oneofschema-3.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.0 kB
Release files / marshmallow_oneofschema-3.2.0.tar.gz
| Download URL | marshmallow_oneofschema-3.2.0.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c06c8d9f14d51ffff152d66d85bd5f27d55cff10752a3b1f8c1f948bf5f597a0
|
|
BLAKE2b-256 checksum How to use checksums |
bb42a0e00dea6a831acfe9d3fe664d695b7cefc02c27dd69d9ccb4bdc3c3d1a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 8, 2025.
Transparency logRelease files / marshmallow_oneofschema-3.2.0-py3-none-any.whl
| Download URL | marshmallow_oneofschema-3.2.0-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
19c87e6124ef05e2831e5c631168c909a50a8fe399921b9841b75fef3785be8c
|
|
BLAKE2b-256 checksum How to use checksums |
711552d6ac14dcfe381e4f1c204c9c287623b8b462bc27c6cc468dba0560ed4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 8, 2025.
Transparency log