Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace try-except block by if-else statement #192

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading