Skip to content

Commit 6133cbe

Browse files
authored
Replace try-except block by if-else statement (#192)
1 parent 5c343c3 commit 6133cbe

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

pydantic_extra_types/color.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ def as_named(self, *, fallback: bool = False) -> str:
124124
if self._rgba.alpha is not None:
125125
return self.as_hex()
126126
rgb = cast(Tuple[int, int, int], self.as_rgb_tuple())
127-
try:
127+
128+
if rgb in COLORS_BY_VALUE:
128129
return COLORS_BY_VALUE[rgb]
129-
except KeyError as e:
130+
else:
130131
if fallback:
131132
return self.as_hex()
132133
else:
133-
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()') from e
134+
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()')
134135

135136
def as_hex(self, format: Literal['short', 'long'] = 'short') -> str:
136137
"""Returns the hexadecimal representation of the color.
@@ -292,12 +293,10 @@ def parse_str(value: str) -> RGBA:
292293
Raises:
293294
ValueError: If the input string cannot be parsed to an RGBA tuple.
294295
"""
296+
295297
value_lower = value.lower()
296-
try:
298+
if value_lower in COLORS_BY_NAME:
297299
r, g, b = COLORS_BY_NAME[value_lower]
298-
except KeyError:
299-
pass
300-
else:
301300
return ints_to_rgba(r, g, b, None)
302301

303302
m = re.fullmatch(r_hex_short, value_lower)

0 commit comments

Comments
 (0)