Add your description here
Project description
OpenEnum
Enum implementation that accepts unknown values.
Rationale
When parsing external data into Python data types, sometimes we deal with enumerated data types which have several documented possible values, but should be parsed with assumption that more possible values will be added over time.
Code that reads such values should be written with forward compatibility in mind: handle all known values, but also include a fallback for any other values that can appear in the future. Options for doing this in Python include parsing as str, as EnumType | str or as EnumType with a _missing_ method defined.
This library attempts to provide a pattern for handling such values in a type-safe manner that plays well with existing linters.
Usage
Parsing possibly-unknown values
Define an OpenEnum subclass with one member value of None that will be used as fallback:
from open_enum import OpenEnum
class Status(OpenEnum):
NEW = "new"
IN_PROGRESS = "in_progress"
DONE = "done"
UNKNOWN = None
Normal values work intuitively:
new = Status.NEW
done = Status.DONE
assert Status("new") == new
assert Status("new") != done
But unlike normal enum, you can also instantiate the type with unknown values:
blocked = Status("blocked")
cancelled = Status("cancelled")
assert isinstance(blocked, Status)
These compare as different to each other:
assert blocked != new
assert blocked != cancelled
but still compare as equal if the underlying value matches:
assert blocked == Status("blocked")
Checking for unknown values using the UNKNOWN marker
The UNKNOWN value defined on the enum type is a special marker object, not an actual enum member:
assert not isinstance(Status.UNKNOWN, Status)
[!NOTE]
You'll never getStatus.UNKNOWNfrom parsing; it's meant to be used explicitly.
The unknown marker compares as equal to unknown values, but not to known values:
assert Status.UNKNOWN == Status("blocked")
assert Status.UNKNOWN == Status("cancelled")
assert Status.UNKNOWN != Status("new")
assert Status.UNKNOWN != Status("in_progress")
Pattern matching
All this allows you to do exhaustive pattern matching that your linter will validate for you:
class Response:
status: Status
...
match response.status:
case Status.NEW:
print("new!")
case Status.IN_PROGRESS:
print("in progress...")
case Status.DONE:
print("nothing more to do")
case Status.UNKNOWN:
print("oh, encountered a status we don't know!")
case _:
assert_never(response.status)
Now if you go back to class Status and add enum members for "blocked" or "cancelled", your type checker will ask you to add corresponding case arms in this match statement.
[!TIP] The main advantage of this pattern over just using
case _as fallback is that is that it communicates the intention that every known value should have acasebranch. Thanks toassert_never, a type checker can actually check and enforce this!
Importantly, as long as you include assert_never, the type checker should nudge you to include a case for Status.UNKNOWN:
match response.status:
case Status.NEW:
print("new!")
case Status.IN_PROGRESS:
print("in progress...")
case Status.DONE:
print("nothing more to do")
case _:
assert_never(response.status) # error: Type "Literal[Status.UNKNOWN]" is not assignable to type "Never"
This way as long as you remember about assert_never(), you'll have a helpful reminder to add handling of additional unknown values that can be added in the future, even if you already handled all values that are currently documented.
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
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 open_enum-0.1.0.tar.gz.
File metadata
- Download URL: open_enum-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d523b33856e1806e37a9b3fc63685055cbb84eecb0c1af7205a0a4848a557058
|
|
| MD5 |
2f9f47c4995fc780889313bcfef1cd3e
|
|
| BLAKE2b-256 |
1b53503d4bd04b90479391d43a26ca41ada7bcf9ab493c888fa5e8372e9cfefa
|
File details
Details for the file open_enum-0.1.0-py3-none-any.whl.
File metadata
- Download URL: open_enum-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f965fe461ad877363a2edd9718aa0dedac06349407e991e7bf42d873c8a51cb
|
|
| MD5 |
9db5c08957e314bf53616bf29869e9ab
|
|
| BLAKE2b-256 |
b59ff2ca41e508a6e269553594ca15e22570f67d69b0d591fde3602d2df7e124
|