Skip to main content

No project description provided

Project description

tagged_dataclasses

Support for tagged unions based on dataclasses via a lightweight mixin that is supported by mypy

from typing import Optional

from dataclasses import dataclass

from tagged_dataclasses import TaggedUnion

class A:
    pass

@dataclass
class AB(A):
    pass

@dataclass
class AC(A):
    pass

@dataclass
class MyUnion(TaggedUnion[A]):
    # Optional is not optional here (this is for better support in PyCharm)
    first: Optional[AB] = None
    second: Optional[AC] = None

x = MyUnion.from_value(AB())

# support for many variations

if x.first is not None:
    pass
elif x.second is not None:
    pass

# other

if x.kind == AB:
    x.value()
elif x.kind == AC:
    x.value()

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

tagged_dataclasses-0.0.2.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

tagged_dataclasses-0.0.2-py3-none-any.whl (3.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page