Skip to content

Commit 80b22ba

Browse files
author
root
committed
edited newtype_enums_int.py and added pics
1 parent 7962492 commit 80b22ba

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

examples/newtype_enum_int_1.png

85.5 KB
Loading

examples/newtype_enum_int_2.png

35.6 KB
Loading

examples/newtype_enum_int_3.png

152 KB
Loading

examples/newtype_enums_int.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ConcreteBoundedInt(cls):
3636

3737

3838
class Severity(GenericWrappedBoundedInt[5], Enum):
39+
# `GenericWrappedBoundedInt` is a `NewType` that wraps an `int` and
40+
# ensures that the value is within the bounds of the enum.
3941
DEBUG = 0
4042
INFO = 1
4143
WARNING = 2
@@ -44,42 +46,54 @@ class Severity(GenericWrappedBoundedInt[5], Enum):
4446

4547

4648
def test_severity():
49+
# Test that enum values map to expected integers
4750
assert Severity.DEBUG == 0
4851
assert Severity.INFO == 1
4952
assert Severity.WARNING == 2
5053
assert Severity.ERROR == 3
5154
assert Severity.CRITICAL == 4
5255

56+
# Test that enum values themselves are immutable
57+
# Should raise AttributeError when trying to modify the enum value directly
5358
with pytest.raises(AttributeError, match=r"[c|C]annot\s+reassign\s+\w+"):
5459
Severity.ERROR += 1
5560

61+
# Test working with enum values as variables
5662
severity = Severity.ERROR
5763
assert severity == 3
5864

65+
# Test incrementing severity level
66+
# When incrementing, should get next enum value
5967
severity += 1
60-
assert severity == 4
61-
assert severity != 3
62-
assert isinstance(severity, int)
63-
assert isinstance(severity, Severity)
64-
assert severity is not Severity.ERROR
65-
assert severity is Severity.CRITICAL
66-
68+
assert severity == 4 # New value should be 4
69+
assert severity != 3 # Old value should no longer match
70+
assert isinstance(severity, int) # Should still be an int
71+
assert isinstance(severity, Severity) # Should still be a Severity
72+
assert severity is not Severity.ERROR # Should no longer be ERROR
73+
assert severity is Severity.CRITICAL # Should now be CRITICAL
74+
75+
# Test decrementing severity level
76+
# When decrementing, should get previous enum value
6777
severity -= 1
68-
assert severity == 3
69-
assert severity != 4
70-
assert isinstance(severity, int)
71-
assert isinstance(severity, Severity)
72-
assert severity is Severity.ERROR
73-
assert severity is not Severity.CRITICAL
74-
78+
assert severity == 3 # New value should be 3
79+
assert severity != 4 # Old value should no longer match
80+
assert isinstance(severity, int) # Should still be an int
81+
assert isinstance(severity, Severity) # Should still be a Severity
82+
assert severity is Severity.ERROR # Should now be ERROR
83+
assert severity is not Severity.CRITICAL # Should no longer be CRITICAL
84+
85+
# Test lower bound handling
7586
severity = Severity.DEBUG
76-
assert severity == 0
87+
assert severity == 0 # DEBUG is lowest severity
7788
assert str(severity.value) == "0"
89+
# Should raise ValueError when trying to go below DEBUG (0)
7890
with pytest.raises(ValueError, match=r"\d+ is not a valid Severity"):
7991
severity -= 1
8092

93+
# Test upper bound handling
8194
severity = Severity.CRITICAL
82-
assert severity == 4
95+
assert severity == 4 # CRITICAL is highest severity
8396
assert str(severity.value) == "4"
97+
# Should raise ValueError when trying to go above CRITICAL (4)
8498
with pytest.raises(ValueError, match=r"\d+ is not a valid Severity"):
8599
severity += 1

0 commit comments

Comments
 (0)