Skip to content

Commit e9a3201

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

2 files changed

Lines changed: 5 additions & 6 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

0 commit comments

Comments
 (0)