Skip to content

Commit 0c10616

Browse files
author
root
committed
added more pics and IntSeverity example
1 parent 80b22ba commit 0c10616

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

examples/newtype_enum_int_4.png

44.8 KB
Loading

examples/newtype_enum_int_5.png

106 KB
Loading

examples/newtype_enums_int.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,56 @@ class Severity(GenericWrappedBoundedInt[5], Enum):
4545
CRITICAL = 4
4646

4747

48+
# IntSeverity demonstrates a basic integer-based enum implementation
49+
# This shows the limitations compared to the NewType-based Severity enum above
50+
class IntSeverity(int, Enum):
51+
# Standard severity levels mapped to integers 0-4
52+
DEBUG = 0 # Lowest severity - debug messages
53+
INFO = 1 # Informational messages
54+
WARNING = 2 # Warning conditions
55+
ERROR = 3 # Error conditions
56+
CRITICAL = 4 # Critical conditions requiring immediate attention
57+
58+
59+
def test_int_severity():
60+
# Basic enum value tests - similar to Severity
61+
assert IntSeverity.DEBUG == 0
62+
assert IntSeverity.INFO == 1
63+
assert IntSeverity.WARNING == 2
64+
assert IntSeverity.ERROR == 3
65+
assert IntSeverity.CRITICAL == 4
66+
67+
# Initial enum value assignment works like Severity
68+
int_severity = IntSeverity.ERROR
69+
assert int_severity == 3
70+
assert int_severity != 4
71+
assert isinstance(int_severity, int)
72+
assert isinstance(int_severity, IntSeverity)
73+
assert int_severity is not IntSeverity.CRITICAL
74+
assert int_severity is IntSeverity.ERROR
75+
76+
# DISADVANTAGE 1: Arithmetic operations break enum type safety
77+
# Unlike Severity which maintains its type after arithmetic,
78+
# IntSeverity degrades to a plain int
79+
int_severity -= 1
80+
assert int_severity == 2
81+
assert int_severity != 3
82+
assert isinstance(int_severity, int)
83+
with pytest.raises(AssertionError):
84+
# DISADVANTAGE 2: Lost enum type after arithmetic
85+
assert isinstance(int_severity, IntSeverity)
86+
87+
# DISADVANTAGE 3: Lost enum identity after arithmetic
88+
assert int_severity is not IntSeverity.ERROR
89+
with pytest.raises(AssertionError):
90+
# Even though value matches WARNING (2), it's not the same object
91+
assert int_severity is IntSeverity.WARNING
92+
93+
# Overall, IntSeverity provides less type safety and consistency
94+
# compared to Severity which maintains its type and identity
95+
# through arithmetic operations
96+
97+
4898
def test_severity():
4999
# Test that enum values map to expected integers
50100
assert Severity.DEBUG == 0

0 commit comments

Comments
 (0)