Type-containing enumeration
Project description
Typenum
This package provide a way to create typed enumerations.
Install
pip install typenumpip install typenum[pydantic]- install with pydantic>=2.9
Quickstart
Without pydantic
class SimpleEnum(TypEnum[TypEnumContent]):
A: type["SimpleEnum"]
Int: type["SimpleEnum[int]"]
# isinstance checking
assert isinstance(SimpleEnum.A(...), SimpleEnum)
assert isinstance(SimpleEnum.Int(123), SimpleEnum.Int)
assert not isinstance(SimpleEnum.Int(123), SimpleEnum.A)
# fully pattern-matching
match SimpleEnum.Int(1):
case SimpleEnum.Int(2):
a = False
case SimpleEnum.Int(1):
a = True
case SimpleEnum.Int():
a = True
case _:
a = False
assert a
With pydantic
from dataclasses import dataclass
from pydantic import BaseModel
from typenum import TypEnum, TypEnumContent
from typenum.pydantic import TypEnumPydantic, FieldMetadata, Rename, SerializationVariants
from typing_extensions import Annotated
@dataclass
class TestDataClass:
a: int
class TestModel(BaseModel):
b: str
class SimpleEnum(TypEnum, TypEnumPydantic):
V1: type["SimpleEnum"]
V2: type["SimpleEnum"]
class OtherEnum(TypEnum[TypEnumContent], TypEnumPydantic):
Int: type["OtherEnum[int]"]
class MyEnum(
TypEnum[TypEnumContent],
TypEnumPydantic,
serialization=SerializationVariants.Nested(), # default value
# serialization=SerializationVariants.Separated("key", "value"),
):
# MyEnum.Int(123)
Int: type["MyEnum[int]"]
# MyEnum.Str(123)
Str: type["MyEnum[str]"]
# MyEnum.Str(OtherEnum.Int(1))
Other: type["MyEnum[OtherEnum]"] # any from OtherEnum variants
# MyEnum.Str(MyEnum.Int(1)) | MyEnum.Str(MyEnum.Str(1))
Self: type["MyEnum[MyEnum]"] # any from self variants
# MyEnum.OnlySelf(...) - any parameters skipped, serialized just by name
OnlySelf: type["MyEnum"]
# MyEnum.OnlySelf2(None)
OnlySelf2: type["MyEnum[None]"]
# MyEnum.List(["1", "2", "3"])
List: type["MyEnum[list[str]]"]
# MyEnum.Dict({"key": "value"})
Dict: type["MyEnum[dict[str, str]]"]
# MyEnum.DC(TestDataClass(a=1))
DC: type["MyEnum[TestDataClass]"]
# MyEnum.Model(TestModel(b="2"))
Model: type["MyEnum[TestModel]"]
# MyEnum.StrTuple(("1", "2")))
StrTuple: Annotated[type["MyEnum[tuple[str, str]]"], FieldMetadata(rename="just_str_tuple")]
# or use typenum.pydantic.Rename
# StrTuple: Annotated[type["MyEnum[tuple[str, str]]"], Rename("some_other_name")]
class FinModel(BaseModel):
enum: MyEnum
def dump_and_load(e: MyEnum):
model = FinModel(enum=e)
json_ = model.model_dump_json()
print(json_)
restored = FinModel.model_validate_json(json_)
assert model == restored
# nested -> {"enum":{"int":1}}
# separated -> {"enum":{"key":"int","value":1}}
dump_and_load(MyEnum.Int(1))
# nested -> {"enum":{"str":"str"}}
# separated -> {"enum":{"key":"str","value":"str"}}
dump_and_load(MyEnum.Str("str"))
# nested -> {"enum":{"list":["list"]}}
# separated -> {"enum":{"key":"list","value":["list"]}}
dump_and_load(MyEnum.List(["list"]))
# nested -> {"enum":{"just_str_tuple":["str","str2"]}}
# separated -> {"enum":{"key":"just_str_tuple","value":["str","str2"]}}
dump_and_load(MyEnum.StrTuple(("str", "str2")))
# nested -> {"enum":{"self":{"int":1}}}
# separated -> {"enum":{"key":"self","value":{"key":"int","value":1}}}
dump_and_load(MyEnum.Self(MyEnum.Int(1)))
# nested -> {"enum":{"dc":{"a":1}}}
# separated -> {"enum":{"key":"dc","value":{"a":1}}}
dump_and_load(MyEnum.DC(TestDataClass(a=1)))
# nested -> {"enum":{"model":{"b":"test_model"}}}
# separated -> {"enum":{"key":"model","value":{"b":"test_model"}}}
dump_and_load(MyEnum.Model(TestModel(b="test_model")))
# nested -> {"enum":{"dict":{"a":"1","b":"2"}}}
# separated -> {"enum":{"key":"dict","value":{"a":"1","b":"2"}}}
dump_and_load(MyEnum.Dict({"a": "1", "b": "2"}))
# nested -> {"enum":"only_self"}
# separated -> {"enum":{"key":"only_self"}}
dump_and_load(MyEnum.OnlySelf(...))
# nested -> {"enum":{"only_self2":null}}
# separated -> {"enum":{"key":"only_self2","value":null}}
dump_and_load(MyEnum.OnlySelf2(None))
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
typenum-0.2.1.tar.gz
(7.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typenum-0.2.1.tar.gz.
File metadata
- Download URL: typenum-0.2.1.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/8.5.0 keyring/24.3.1 pkginfo/1.11.2 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.32.3 rfc3986/1.5.0 tqdm/4.57.0 urllib3/2.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89c4828c4ab3706c09bd565a59ed5934319a170b52c07b5f10e0fb99935fe929
|
|
| MD5 |
e82a2d20f2909de757eef8a1dbd5082d
|
|
| BLAKE2b-256 |
0bd9e81407224869a3b79ed83d5425c06ce4f43cecb45ea6517a193611c1260a
|
File details
Details for the file typenum-0.2.1-py3-none-any.whl.
File metadata
- Download URL: typenum-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/8.5.0 keyring/24.3.1 pkginfo/1.11.2 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.32.3 rfc3986/1.5.0 tqdm/4.57.0 urllib3/2.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22c550e6b69bfc0dd1578e6a43346f08314c6a6ee85e1367b639276e4568ae07
|
|
| MD5 |
4bd4a41ed23b5f4cf29080847843ae3a
|
|
| BLAKE2b-256 |
783a30cfa832428445a4b0cef6b84a6be7d43e2bfcadd0a8c76477848ece088b
|