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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file tagged_dataclasses-0.0.2.tar.gz.

File metadata

  • Download URL: tagged_dataclasses-0.0.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for tagged_dataclasses-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6a08929387e3b04a0667f8457caf5a98a3ec5a904a4b2781c5c23fd006a1fbcb
MD5 671041d53dd585cb5deab5c0365f7709
BLAKE2b-256 ac5499d2d17523ace3ff43b67b1e094080d272ccca14ed164ff4c845d26f8102

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tagged_dataclasses-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for tagged_dataclasses-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 04600afefe4befd668489753c6ebaf2c95ca348fce2092e01c87d45b046c8f0c
MD5 f4db82730347666849fd7e7e8d191d45
BLAKE2b-256 164a1fcc18c0da12f6193043dac371bedcec4b06afc2b454e2a0622055ca76bb

See more details on using hashes here.

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