Concise sum types in Python.
Project description
type_enum
: Concise sum types in Python
Installation
pip install type-enum
And for the mypy plugin:
pip install type-enum-plugin
Then register the plugin with
[tool.mypy]
plugins = "type_enum_plugin"
Usage
from type_enum import Field, TypeEnum
class BgColor(TypeEnum):
transparent: Field[()]
name: Field[str]
rgb: Field[int, int, int]
hsv: Field[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"
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.3.0.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for type_enum-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fce17c3a66ed26eaee26032c386b6ae5ac93343fb9d0788941e5529d2851df3c |
|
MD5 | d8f29ec0e99673c1db5cc27132234631 |
|
BLAKE2b-256 | b65625d40a0e31c11da00574d6bb07020c830f6f5090c51d748d14af17d54f5b |