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
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
Close
Hashes for tagged_dataclasses-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04600afefe4befd668489753c6ebaf2c95ca348fce2092e01c87d45b046c8f0c |
|
MD5 | f4db82730347666849fd7e7e8d191d45 |
|
BLAKE2b-256 | 164a1fcc18c0da12f6193043dac371bedcec4b06afc2b454e2a0622055ca76bb |