examples.16_enum_config_errors_no_throw
Demonstrates how EnumValue handles configuration errors gracefully
by reporting Status.EXCEPTION rather than throwing runtime exceptions.
This example shows:
- Behavior when an
EnumValueis initialized with an empty Enum class.- Behavior when an
EnumValueis initialized with an empty allowed sequence.- In both cases,
status = EXCEPTIONand a descriptive error message appears indetails.
Run directly:
python examples/16_enum_config_errors_no_throw.py
Expected output:
Empty Enum: EXCEPTION - Enum has no members. Empty sequence: EXCEPTION - Must be a non-empty sequence.
1"""Demonstrates how :class:`EnumValue` handles configuration errors gracefully 2by reporting `Status.EXCEPTION` rather than throwing runtime exceptions. 3 4This example shows: 5 * Behavior when an `EnumValue` is initialized with an empty Enum class. 6 * Behavior when an `EnumValue` is initialized with an empty allowed sequence. 7 * In both cases, `status = EXCEPTION` and a descriptive error message 8 appears in `details`. 9 10Run directly: 11 12 python examples/16_enum_config_errors_no_throw.py 13 14Expected output: 15 16 Empty Enum: EXCEPTION - Enum has no members. 17 Empty sequence: EXCEPTION - Must be a non-empty sequence. 18""" 19 20import sys 21import pathlib 22from enum import Enum 23 24# --------------------------------------------------------------------------- 25# Make repo root importable when running this file directly 26# --------------------------------------------------------------------------- 27sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 28 29from constrained_values import EnumValue 30 31 32class Empty(Enum): 33 """An empty enumeration with no members.""" 34 pass 35 36 37def main() -> None: 38 """Run the EnumValue configuration error demonstration. 39 40 Creates two misconfigured :class:`EnumValue` instances to show how 41 invalid configurations are handled without raising exceptions. 42 43 Steps: 44 1. Instantiate `EnumValue("anything", Empty)` → empty Enum class. 45 2. Instantiate `EnumValue("x", [])` → empty allowed value sequence. 46 47 Both cases produce `status = Status.EXCEPTION`, and error details 48 are provided via the `.details` field. 49 50 Prints: 51 * `"Empty Enum: EXCEPTION - Enum has no members."` 52 * `"Empty sequence: EXCEPTION - Must be a non-empty sequence."` 53 """ 54 a = EnumValue("anything", Empty) 55 b = EnumValue("x", []) 56 print("Empty Enum:", a.status.name, "-", a.details) 57 print("Empty sequence:", b.status.name, "-", b.details) 58 59 60if __name__ == "__main__": 61 main()
class
Empty(enum.Enum):
An empty enumeration with no members.
Inherited Members
- enum.Enum
- name
- value
def
main() -> None:
38def main() -> None: 39 """Run the EnumValue configuration error demonstration. 40 41 Creates two misconfigured :class:`EnumValue` instances to show how 42 invalid configurations are handled without raising exceptions. 43 44 Steps: 45 1. Instantiate `EnumValue("anything", Empty)` → empty Enum class. 46 2. Instantiate `EnumValue("x", [])` → empty allowed value sequence. 47 48 Both cases produce `status = Status.EXCEPTION`, and error details 49 are provided via the `.details` field. 50 51 Prints: 52 * `"Empty Enum: EXCEPTION - Enum has no members."` 53 * `"Empty sequence: EXCEPTION - Must be a non-empty sequence."` 54 """ 55 a = EnumValue("anything", Empty) 56 b = EnumValue("x", []) 57 print("Empty Enum:", a.status.name, "-", a.details) 58 print("Empty sequence:", b.status.name, "-", b.details)
Run the EnumValue configuration error demonstration.
Creates two misconfigured EnumValue instances to show how
invalid configurations are handled without raising exceptions.
Steps:
- Instantiate
EnumValue("anything", Empty)→ empty Enum class.- Instantiate
EnumValue("x", [])→ empty allowed value sequence.
Both cases produce status = Status.EXCEPTION, and error details
are provided via the .details field.
Prints:
"Empty Enum: EXCEPTION - Enum has no members.""Empty sequence: EXCEPTION - Must be a non-empty sequence."