Skip to content

Commit

Permalink
Replace try-except block by if-else statement (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsos committed Jun 23, 2024
1 parent 5c343c3 commit 6133cbe
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pydantic_extra_types/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ def as_named(self, *, fallback: bool = False) -> str:
if self._rgba.alpha is not None:
return self.as_hex()
rgb = cast(Tuple[int, int, int], self.as_rgb_tuple())
try:

if rgb in COLORS_BY_VALUE:
return COLORS_BY_VALUE[rgb]
except KeyError as e:
else:
if fallback:
return self.as_hex()
else:
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()') from e
raise ValueError('no named color found, use fallback=True, as_hex() or as_rgb()')

def as_hex(self, format: Literal['short', 'long'] = 'short') -> str:
"""Returns the hexadecimal representation of the color.
Expand Down Expand Up @@ -292,12 +293,10 @@ def parse_str(value: str) -> RGBA:
Raises:
ValueError: If the input string cannot be parsed to an RGBA tuple.
"""

value_lower = value.lower()
try:
if value_lower in COLORS_BY_NAME:
r, g, b = COLORS_BY_NAME[value_lower]
except KeyError:
pass
else:
return ints_to_rgba(r, g, b, None)

m = re.fullmatch(r_hex_short, value_lower)
Expand Down

0 comments on commit 6133cbe

Please sign in to comment.