Skip to content

Commit

Permalink
fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Nov 10, 2023
1 parent 0946c37 commit 3669ee0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
44 changes: 25 additions & 19 deletions faker/providers/color/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,32 +291,38 @@ def get_minimum_brightness(self, h: int, s: int) -> int:

return 0

def _validate_color_input(self, color_input: HueType) -> Tuple[int, int]:
if (
not isinstance(color_input, (list, tuple))
or len(color_input) != 2
or any(not isinstance(c, (float, int)) for c in color_input)
):
raise TypeError("Hue must be a valid string, numeric type, or a tuple/list of 2 numeric types.")

return color_input[0], color_input[1]

def get_hue_range(self, color_input: Optional[HueType]) -> Tuple[int, int]:
"""Return the hue range for a given ``color_input``."""
if color_input is None:
return 0, 360

if isinstance(color_input, (int, float)) and 0 <= color_input <= 360:
color_input = int(color_input)
return color_input, color_input
elif isinstance(color_input, str) and color_input in self.colormap:

if isinstance(color_input, str) and color_input in self.colormap:
return self.colormap[color_input]["hue_range"][0]
elif color_input is None:
return 0, 360

if isinstance(color_input, list):
color_input = tuple(color_input)
if (
isinstance(color_input, tuple)
and len(color_input) == 2
and all(isinstance(c, (float, int)) for c in color_input)
):
v1 = int(color_input[0])
v2 = int(color_input[1])

if v2 < v1:
v1, v2 = v2, v1
v1 = max(v1, 0)
v2 = min(v2, 360)
return v1, v2
raise TypeError("Hue must be a valid string, numeric type, or a tuple/list of 2 numeric types.")
color_input = self._validate_color_input(color_input)

v1 = int(color_input[0])
v2 = int(color_input[1])

if v2 < v1:
v1, v2 = v2, v1
v1 = max(v1, 0)
v2 = min(v2, 360)
return v1, v2

def get_saturation_range(self, hue: int) -> Tuple[int, int]:
"""Return the saturation range for a given numerical ``hue`` value."""
Expand Down
2 changes: 1 addition & 1 deletion faker/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing_extensions import OrderedDict as OrderedDictType # NOQA

DateParseType = Union[date, datetime, timedelta, str, int]
HueType = TypeVar("HueType", str, float, Sequence[int])
HueType = Union[str, float, int, Sequence[int]]
SexLiteral = Literal["M", "F"]
SeedType = Union[int, float, str, bytes, bytearray, None]

Expand Down

0 comments on commit 3669ee0

Please sign in to comment.