Skip to main content

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

  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.1.tar.gz (6.6 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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file marshmallow_tagged_union-0.0.1.tar.gz.

File metadata

  • Download URL: marshmallow_tagged_union-0.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for marshmallow_tagged_union-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3226bbc226a4867ff1c42d442c47381353ca7cf760c42212552f68a69a9ef73b
MD5 3d120ff8caf3646aec06f800fa60c8f7
BLAKE2b-256 a8745da34ac72d237fa209c927945099f986fdb361554c1fec517e2bee6a4196

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for marshmallow_tagged_union-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 659ac8e1c90cab47349b4cf95eb4767990f6fee5676e92dff51ad6e56e636dfc
MD5 7a63972fc3d87c7151ceb744360a3bd0
BLAKE2b-256 fc9a551bf2d30e62db51818c93c2dc4622132f85015398205b04ec1181dc0a3c

See more details on using hashes here.

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