Provide support for tagged unions in marshmallow schemas
Project description
marshmallow-tagged-union
Provide support for tagged unions (discriminated unions) in marshmallow schemas.
Installation
pip install marshmallow-tagged-union
What is a Tagged Union?
A tagged union (also known as a discriminated union or variant) is a data structure that can hold values of different types, with a "tag" field that indicates which type is currently stored. This is useful for modeling polymorphic data where you have a common base structure with type-specific fields.
Usage
Basic Example
from enum import Enum
from marshmallow import fields
from marshmallow_tagged_union import TagUnionSchema
class ShapeType(Enum):
CIRCLE = "circle"
RECTANGLE = "rectangle"
# Define the base union schema
class ShapeSchema(TagUnionSchema):
name = fields.String(required=True) # Common field
shape_type = fields.Enum(ShapeType, required=True, by_value=True)
class Meta(TagUnionSchema.Meta):
tag_field = "shape_type" # Field that discriminates the type
# Define specific union members
class CircleSchema(ShapeSchema):
shape_type = ShapeType.CIRCLE
radius = fields.Float(required=True)
class Meta(ShapeSchema.Meta):
pass
class RectangleSchema(ShapeSchema):
shape_type = ShapeType.RECTANGLE
width = fields.Float(required=True)
height = fields.Float(required=True)
class Meta(ShapeSchema.Meta):
pass
# Usage: Deserialize (load)
schema = ShapeSchema()
circle_data = {
"name": "My Circle",
"shape_type": "circle",
"radius": 5.0
}
result = schema.load(circle_data)
# Returns: {'name': 'My Circle', 'shape_type': <ShapeType.CIRCLE: 'circle'>, 'radius': 5.0}
# Usage: Serialize (dump)
rectangle_data = {
"name": "My Rectangle",
"shape_type": ShapeType.RECTANGLE,
"width": 10.0,
"height": 20.0
}
result = schema.dump(rectangle_data)
# Returns: {'name': 'My Rectangle', 'shape_type': 'rectangle', 'width': 10.0, 'height': 20.0}
Key Concepts
- Base Schema: Inherit from
TagUnionSchemaand define common fields shared by all union members - Tag Field: Set
tag_fieldin theMetaclass to specify which field discriminates between types - Member Schemas: Inherit from the base schema and:
- Set the tag field to a specific value (e.g.,
shape_type = ShapeType.CIRCLE) - Add type-specific fields (e.g.,
radiusfor circles)
- Set the tag field to a specific value (e.g.,
Features
- Automatic type resolution: The correct schema is automatically selected based on the tag field value during deserialization
- Validation: All marshmallow validation features work on both common and type-specific fields
- Common fields: Define fields once in the base schema and they're available in all union members
- Type safety: Use Python enums for type-safe tag values
Development
Install in Development Mode
pip install --editable ".[dev]"
Run Tests
pytest
Building
To build the distribution packages:
pip install build
python -m build
This will create both wheel (.whl) and source distribution (.tar.gz) files in the dist/ directory.
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file marshmallow_tagged_union-0.0.2.tar.gz.
File metadata
- Download URL: marshmallow_tagged_union-0.0.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
321e9fd0e83e41cf97a2e5bbd97cf118db2dc6cd4a82a29d89ed5af7745d2271
|
|
| MD5 |
c8ee445b9c6382a9f184e3c3ba61f875
|
|
| BLAKE2b-256 |
4c9e79b2ac7652b5e2173e879ba49e6a3608ff3bc7edd12eec112b76431ffc62
|
Provenance
The following attestation bundles were made for marshmallow_tagged_union-0.0.2.tar.gz:
Publisher:
publish.yml on The-Gopher/marshmallow-tagged-union
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
marshmallow_tagged_union-0.0.2.tar.gz -
Subject digest:
321e9fd0e83e41cf97a2e5bbd97cf118db2dc6cd4a82a29d89ed5af7745d2271 - Sigstore transparency entry: 850122860
- Sigstore integration time:
-
Permalink:
The-Gopher/marshmallow-tagged-union@97e9bd97dfdd6eaf5384a3ef6b0315eab1e6e88c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/The-Gopher
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97e9bd97dfdd6eaf5384a3ef6b0315eab1e6e88c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file marshmallow_tagged_union-0.0.2-py3-none-any.whl.
File metadata
- Download URL: marshmallow_tagged_union-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
665167b139cd01a5313e7b8beacee353f57fa1805b37f1c54fed0ad071a6b6b0
|
|
| MD5 |
a8cdce4a0ddc8e67bdc719be54cdefa5
|
|
| BLAKE2b-256 |
2dfd37f5e72c1212134e500fa6ce55c70c9c772da7cd721449b5795b74200795
|
Provenance
The following attestation bundles were made for marshmallow_tagged_union-0.0.2-py3-none-any.whl:
Publisher:
publish.yml on The-Gopher/marshmallow-tagged-union
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
marshmallow_tagged_union-0.0.2-py3-none-any.whl -
Subject digest:
665167b139cd01a5313e7b8beacee353f57fa1805b37f1c54fed0ad071a6b6b0 - Sigstore transparency entry: 850122864
- Sigstore integration time:
-
Permalink:
The-Gopher/marshmallow-tagged-union@97e9bd97dfdd6eaf5384a3ef6b0315eab1e6e88c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/The-Gopher
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@97e9bd97dfdd6eaf5384a3ef6b0315eab1e6e88c -
Trigger Event:
workflow_dispatch
-
Statement type: