Skip to main content

Provide support for tagged unions in marshmallow schemas

Project description

marshmallow-tagged-union

PyPI version

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

  1. Base Schema: Inherit from TagUnionSchema and define common fields shared by all union members
  2. Tag Field: Set tag_field in the Meta class to specify which field discriminates between types
  3. 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., radius for circles)

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


Download files

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

Source Distribution

marshmallow_tagged_union-0.0.2.tar.gz (7.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_tagged_union-0.0.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

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

Hashes for marshmallow_tagged_union-0.0.2.tar.gz
Algorithm Hash digest
SHA256 321e9fd0e83e41cf97a2e5bbd97cf118db2dc6cd4a82a29d89ed5af7745d2271
MD5 c8ee445b9c6382a9f184e3c3ba61f875
BLAKE2b-256 4c9e79b2ac7652b5e2173e879ba49e6a3608ff3bc7edd12eec112b76431ffc62

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marshmallow_tagged_union-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_tagged_union-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 665167b139cd01a5313e7b8beacee353f57fa1805b37f1c54fed0ad071a6b6b0
MD5 a8cdce4a0ddc8e67bdc719be54cdefa5
BLAKE2b-256 2dfd37f5e72c1212134e500fa6ce55c70c9c772da7cd721449b5795b74200795

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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