Concise sum types in Python.
Reason this release was yanked:
old syntax
Project description
type_enum
: Concise sum types in Python
Installation
pip install type-enum
And for the mypy plugin:
pip install type-enum-plugin
Usage
from type_enum import TypeEnum
class BgColor(TypeEnum):
transparent = ()
name = (str,)
rgb = (int, int, int)
hsv = (int, int, int)
background_color: BgColor = BgColor.rgb(39, 127, 168)
assert isinstance(background_color, BgColor)
assert not isinstance(BgColor.rgb, BgColor) # different from Enum
match background_color:
case BgColor.transparent():
print("no color")
case BgColor.name(color_name):
print(f"color name: {color_name}")
case BgColor.rgb(red, green, blue):
print(f"RGB: {red}, {green}, {blue}")
case BgColor.hsv(hue, saturation, value):
print(f"HSV: {hue}, {saturation}, {value}")
# will print "RGB: 39, 127, 168"
You can also specify field names by using a dictionary instead of a tuple:
from type_enum import TypeEnum
class BgColor(TypeEnum):
transparent = ()
name = (str,)
rgb = {"red": int, "green": int, "blue": int} # named args
hsv = {"hue": int, "saturation": int, "value": int}
background_color = BgColor.rgb(red=39, green=127, blue=168)
assert isinstance(background_color, BgColor)
match background_color:
case BgColor.transparent():
print("no color")
case BgColor.name(color_name):
print(f"color name: {color_name}")
case BgColor.rgb(red=r, green=g, blue=b):
print(f"RGB: {r}, {g}, {b}")
case BgColor.hsv(hue=h, saturation=s, value=v):
print(f"HSV: {h}, {s}, {v}")
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
type_enum-0.2.1.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for type_enum-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09d6d7af8e42a1a2e4caff4f0779c511a44f4380e891d9ffb8615b5a70617626 |
|
MD5 | 9da4cf993ebb1ad181d73a19706700aa |
|
BLAKE2b-256 | ce2a0b1d55d48d9eb29469f7a1b6ff8219493d029a83c792e4b6ad13d71dfa7b |