FBEnum
Library provides fallback enum functionality. Fully compatible with pydantic.
It can be helpful to have a fallback value for enums, so that unknown values can be handled gracefully. This is especially useful when working with external data sources that may contain values that are not known at the time of writing the code.
Installation
pip install fbenum
Usage
fbenum provides two possible ways to use it:
- As a class mixin, which can be used to enhance any enum class
- As a pydantic validator, which can be used to validate enum values in pydantic models
Class mixin
Class mixin can be used to enhance any enum class with fallback functionality.
from enum import Enum
from fbenum.enum import FallbackEnum
@enum.unique
class MyEnum(enum.StrInt, FallbackEnum):
__unknown_name__ = 'MISSING'
A = 1
B = 2
assert MyEnum(1) == MyEnum.A
assert MyEnum(44).name == 'MISSING'
assert MyEnum(44).value == 44
Pydantic validator
As a pydantic validator, fbenum can be used to validate enum values in pydantic models.
In this cases, the enum class no longer needs to be a subclass of FallbackEnum or somehow
otherwise enhanced.
Two ways of using it are provided:
Annotated type validator
from enum import Enum
from fbenum.adapter import FallbackAdapter
@enum.unique
class MyEnum(enum.StrInt):
A = 1
B = 2
class MyModel(BaseModel):
value: Annotated[
MyEnum, FallbackAdapter(unknown_name='MISSING'),
]
model = MyModel(value=1)
assert model.value == MyEnum.A
model = MyModel(value=44)
assert model.value.name == 'MISSING'
assert model.value.value == 44
Type wrapper
Type wrapper has restricted functionality, as it can only be used to wrap a single enum class, and it does not support any additional arguments. However, it is more concise.
from enum import Enum
from fbenum.adapter import FallbackAdapter
@enum.unique
class MyEnum(enum.StrInt):
A = 1
B = 2
class MyModel(BaseModel):
value: FallbackAdapter[MyEnum]
model = MyModel(value=1)
assert model.value == MyEnum.A
model = MyModel(value=44)
assert model.value.name == 'MISSING'
assert model.value.value == 44
Or a more realistic and complex example:
from enum import Enum
from fbenum.adapter import FallbackAdapter
@enum.unique
class UserStatus(enum.StrInt):
"""User status."""
ACTIVE = 1
INACTIVE = 2
class UserRequest(BaseModel):
name: str
email: str
status: FallbackAdapter[UserStatus]
class UserResponse(BaseModel):
name: str
email: str
status: FallbackAdapter[UserStatus]
def create_user(request: UserRequest) -> UserResponse:
"""Create a user."""
# Do some stuff
resp = ...
return UserResponse.validate_model(resp)
def get_user(user_id: int) -> UserResponse:
"""Get a user."""
# Do some stuff
resp = ...
return UserResponse.validate_model(resp)
def get_users() -> list[UserResponse]:
"""Get all users."""
# Do some stuff
resp = ...
return UserResponse.validate_model(resp)
if __name__ == '__main__':
user = create_user(
UserRequest(
name='John Doe',
email='test@emaik.com',
status=1,
),
)
# on user cretion status always will be known
users = get_users()
# user == [
# UserResponse(
# name='John Doe',
# email='test@emaik',
# status=1,
# ),
# UserResponse(
# name='Jane Doe',
# email='test2@emaik',
# status=3, # unknown status
# ]
# on users_getting status can be unknown, so using fallback wrapper for response
# model will be a good idea to handle unknown values gracefully.
Metadata
Release files for fbenum 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fbenum-1.0.1.tar.gz | 10.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fbenum-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.2 kB
Release files / fbenum-1.0.1.tar.gz
| Download URL | fbenum-1.0.1.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2f2606dbd31318ff078a2e3cbf1bcecff6ea215d3f8ed2f626c6207dc5103ba
|
|
BLAKE2b-256 checksum How to use checksums |
0220319b3fabc1e52c3c11f9a8d35355b1013bc6a8fcbfc3d6202161eae6cbe7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.12.1 CPython/3.10.12
|
Release files / fbenum-1.0.1-py3-none-any.whl
| Download URL | fbenum-1.0.1-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5399582bfca2c8eb84b0836bade371f7f3408cbf23af4ec398fdc25ca98da434
|
|
BLAKE2b-256 checksum How to use checksums |
da821bc16f7eac0aa61ed6ba368f807ddcd2dedfa6600a30c86c86a274373ae1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.12.1 CPython/3.10.12
|
Security for fbenum 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.