Skip to content

Commit 28ac6ba

Browse files
committed
Enum: Replace ValueError to AttributeError.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent ff1eef4 commit 28ac6ba

4 files changed

Lines changed: 18 additions & 18 deletions

File tree

python-stdlib/enum/enum.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,4 @@ The base class for all enumerations.
113113
* Raised when attempting to modify an `EnumValue`.
114114
* Raised when attempting to add new members to an initialized Enum.
115115
* Raised when a class-level lookup (`Status(999)`) fails.
116-
* **`ValueError`**:
117116
* Raised when an instance-level lookup (`s(999)`) fails.

python-stdlib/enum/enum.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _lookup(cls, value):
6565
# Wrap static numbers found in class definition
6666
return EnumValue(attr, key)
6767

68-
raise AttributeError(f"{value} is not in {cls.__name__} enum")
68+
raise AttributeError(f"{value} is not in {cls.__name__}")
6969

7070
def list_members(self):
7171
# Returns a list of tuples (name, value) for all members
@@ -103,7 +103,7 @@ def __call__(self, value):
103103
for member in self:
104104
if member.value == value:
105105
return member
106-
raise ValueError(f"no such value: {value}")
106+
raise AttributeError(f"{value} is not in {self.__class__.__name__}")
107107

108108
def __setattr__(self, key, value):
109109
if hasattr(self, '_initialized'):
@@ -139,7 +139,7 @@ class Color(Enum):
139139

140140
# Create instance
141141
c = Color()
142-
print(f"Enum repr c: {c}")
142+
print(f"Enum c: {c}")
143143

144144
# Basic access
145145
print(f"RED: Name={c.RED.name}, Value={c.RED.value}, EnumValue={c.RED}, Call={c.RED()} ")
@@ -162,8 +162,8 @@ class Color(Enum):
162162

163163
try:
164164
c(999)
165-
except ValueError as e:
166-
print(f"\nValueError: {c} {e}\n")
165+
except AttributeError as e:
166+
print(f"\nAttributeError: {e}: {c}\n")
167167

168168
# --- Usage Example 2 ---
169169
# Define an Enum class

python-stdlib/enum/test_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_call_reverse_lookup(self):
3838
self.assertEqual(result.value, 1)
3939

4040
# Перевірка виключення для неіснуючого значення
41-
with self.assertRaises(ValueError):
41+
with self.assertRaises(AttributeError):
4242
self.color(999)
4343

4444
def test_is_value(self):

python-stdlib/enum/test_enum_.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import unittest
55
from enum import Enum, EnumValue
66

7-
class TestEnum(unittest.TestCase):
87

8+
class TestEnum(unittest.TestCase):
99
def setUp(self):
1010
# Define standard Enum classes for testing
1111
class Color(Enum):
1212
RED = 'red'
1313
GREEN = 'green'
1414
BLUE = 'blue'
15-
15+
1616
class Status(Enum):
1717
IDLE = 0
1818
RUNNING = 1
@@ -42,7 +42,7 @@ def test_immutability(self):
4242
# Test EnumValue immutability
4343
with self.assertRaises(AttributeError):
4444
self.color_inst.RED.value = 'blue'
45-
45+
4646
# Test Enum instance immutability (once initialized)
4747
with self.assertRaises(AttributeError):
4848
self.color_inst.NEW_COLOR = 'yellow'
@@ -63,24 +63,25 @@ def test_reverse_lookup_via_instance_call(self):
6363
member = self.color_inst('green')
6464
self.assertEqual(member.name, 'GREEN')
6565

66-
with self.assertRaises(ValueError):
66+
with self.assertRaises(AttributeError):
6767
self.color_inst('yellow')
6868

6969
def test_iteration(self):
7070
"""Test that the Enum instance is iterable and returns all members."""
7171
members = list(self.status_inst)
7272
names = [m.name for m in members]
7373
values = [m.value for m in members]
74-
74+
7575
self.assertEqual(len(members), 3)
7676
self.assertIn('IDLE', names)
7777
self.assertIn(2, values)
7878

79+
7980
# def test_functional_api(self):
8081
# """Test dynamic Enum creation using the Functional API."""
8182
# # Dynamic creation via Enum(name, names_dict)
8283
# State = Enum(name='State', names={'ON': 1, 'OFF': 0})
83-
#
84+
#
8485
# self.assertTrue(hasattr(State, 'ON'))
8586
# self.assertEqual(State.ON.value, 1)
8687
# self.assertEqual(State.OFF.name, 'OFF')
@@ -92,18 +93,18 @@ def test_iteration(self):
9293
# print('c_repr', c_repr)
9394
# c_restored = eval(c_repr)
9495
# self.assertEqual(self.color_inst, c_restored)
95-
#
96+
#
9697
# # Test dynamically created instance
9798
# s_dynamic = Enum(name='State', names={'ON': 1, 'OFF': 0})
9899
# s_repr = repr(s_dynamic)
99100
# s_restored = eval(s_repr)
100101
# self.assertEqual(s_dynamic, s_restored)
101102

102-
def test_len_and_list_members(self):
103-
"""Test utility functions like __len__ and list_members."""
103+
def test_len_and_list(self):
104+
"""Test utility functions like __len__ and list."""
104105
self.assertEqual(len(self.color_inst), 3)
105-
members_list = self.color_inst.list_members()
106-
self.assertEqual(members_list, [('BLUE', 'blue'), ('GREEN', 'green'), ('RED', 'red')])
106+
members_list = self.color_inst.list()
107+
self.assertEqual(members_list, [self.color_inst.BLUE, self.color_inst.GREEN, self.color_inst.RED])
107108

108109
def test_deletion_prevention(self):
109110
"""Verify that members cannot be deleted."""

0 commit comments

Comments
 (0)