Skip to content

Commit

Permalink
TYP: Reduce number of type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Oct 21, 2024
1 parent a08fa1a commit a27e803
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 154 deletions.
6 changes: 3 additions & 3 deletions mizani/_colors/_colormaps/_interpolated.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ._colormap import ColorMap, ColorMapKind

if TYPE_CHECKING:
from typing import Optional, Sequence
from typing import Sequence

from mizani.typing import (
FloatArrayLike,
Expand Down Expand Up @@ -51,7 +51,7 @@ def _generate_colors(self, x: FloatArrayLike) -> Sequence[RGBHexColor]:
@dataclass
class InterpolatedMap(_InterpolatedGen):
colors: Sequence[RGBHexColor] | Sequence[RGBColor] | RGBColorArray
values: Optional[Sequence[float]] = None
values: Sequence[float] | None = None
kind: ColorMapKind = ColorMapKind.miscellaneous

def __post_init__(self):
Expand Down Expand Up @@ -93,7 +93,7 @@ def __post_init__(self):
def interp_lookup(
x: NDArrayFloat,
values: NDArrayFloat,
values_alt: Optional[NDArrayFloat] = None,
values_alt: NDArrayFloat | None = None,
) -> NDArrayFloat:
"""
Create an interpolation lookup array
Expand Down
37 changes: 20 additions & 17 deletions mizani/_core/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
if TYPE_CHECKING:
from mizani.typing import (
DatetimeBreaksUnits,
TupleDatetime2,
TupleFloat2,
TupleInt2,
)


Expand Down Expand Up @@ -145,31 +142,33 @@ def u(self) -> int:
return math.floor(self._tdelta.total_seconds() * 1e6)

@property
def limits(self) -> TupleDatetime2:
def limits(self) -> tuple[datetime, datetime]:
return self.start, self.end

def limits_year(self) -> TupleDatetime2:
def limits_year(self) -> tuple[datetime, datetime]:
return floor_year(self.start), ceil_year(self.end)

def limits_month(self) -> TupleDatetime2:
def limits_month(self) -> tuple[datetime, datetime]:
return round_month(self.start), round_month(self.end)

def limits_week(self) -> TupleDatetime2:
def limits_week(self) -> tuple[datetime, datetime]:
return floor_week(self.start), ceil_week(self.end)

def limits_day(self) -> TupleDatetime2:
def limits_day(self) -> tuple[datetime, datetime]:
return floor_day(self.start), ceil_day(self.end)

def limits_hour(self) -> TupleDatetime2:
def limits_hour(self) -> tuple[datetime, datetime]:
return floor_hour(self.start), ceil_hour(self.end)

def limits_minute(self) -> TupleDatetime2:
def limits_minute(self) -> tuple[datetime, datetime]:
return floor_minute(self.start), ceil_minute(self.end)

def limits_second(self) -> TupleDatetime2:
def limits_second(self) -> tuple[datetime, datetime]:
return floor_second(self.start), ceil_second(self.end)

def limits_for_frequency(self, freq: DateFrequency) -> TupleDatetime2:
def limits_for_frequency(
self, freq: DateFrequency
) -> tuple[datetime, datetime]:
lookup = {
DF.YEARLY: self.limits_year,
DF.MONTHLY: self.limits_month,
Expand Down Expand Up @@ -389,7 +388,7 @@ def at_the_second(d: datetime) -> bool:
return d.time().microsecond == 0


def align_limits(limits: TupleInt2, width: float) -> TupleFloat2:
def align_limits(limits: tuple[int, int], width: float) -> tuple[float, float]:
"""
Return limits so that breaks should be multiples of the width
Expand All @@ -409,8 +408,10 @@ def align_limits(limits: TupleInt2, width: float) -> TupleFloat2:


def shift_limits_down(
candidate_limits: TupleInt2, original_limits: TupleInt2, width: int
) -> TupleInt2:
candidate_limits: tuple[int, int],
original_limits: tuple[int, int],
width: int,
) -> tuple[int, int]:
"""
Shift candidate limits down so that they can be a multiple of width
Expand Down Expand Up @@ -438,8 +439,10 @@ def shift_limits_down(


def expand_datetime_limits(
limits: TupleDatetime2, width: int, units: DatetimeBreaksUnits
) -> TupleDatetime2:
limits: tuple[datetime, datetime],
width: int,
units: DatetimeBreaksUnits,
) -> tuple[datetime, datetime]:
ival = Interval(*limits)
if units == "year":
start, end = ival.limits_year()
Expand Down
11 changes: 5 additions & 6 deletions mizani/_core/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .types import DateFrequency, date_breaks_info

if TYPE_CHECKING:
from typing import Generator, Optional, Sequence
from typing import Generator, Sequence

from mizani.typing import (
Datetime,
Expand All @@ -26,7 +26,6 @@
SeqDatetime,
Timedelta,
TimedeltaArrayLike,
TzInfo,
)


Expand Down Expand Up @@ -81,7 +80,7 @@ def _from_ordinalf(x: float, tz: tzinfo | None) -> datetime:
_from_ordinalf_np_vectorized = np.vectorize(_from_ordinalf, otypes="O")


def get_tzinfo(tz: Optional[str | TzInfo] = None) -> TzInfo | None:
def get_tzinfo(tz: str | tzinfo | None = None) -> tzinfo | None:
"""
Generate `~datetime.tzinfo` from a string or return `~datetime.tzinfo`.
Expand Down Expand Up @@ -146,7 +145,7 @@ def datetime64_to_num(x: NDArrayDatetime) -> NDArrayFloat:


def num_to_datetime(
x: FloatArrayLike, tz: Optional[str | TzInfo] = None
x: FloatArrayLike, tz: str | tzinfo | None = None
) -> NDArrayDatetime:
"""
Convert any float array to numpy datetime64 array
Expand Down Expand Up @@ -338,8 +337,8 @@ def calculate_date_breaks_byunits(
limits,
units: DatetimeBreaksUnits,
width: int,
max_breaks: Optional[int] = None,
tz: Optional[TzInfo] = None,
max_breaks: int | None = None,
tz: tzinfo | None = None,
) -> Sequence[datetime]:
"""
Calcuate date breaks using appropriate units
Expand Down
6 changes: 2 additions & 4 deletions mizani/_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import dateutil.rrule as rr

if TYPE_CHECKING:
from mizani.typing import (
TzInfo,
)
from datetime import tzinfo


class DateFrequency(IntEnum):
Expand Down Expand Up @@ -44,4 +42,4 @@ class date_breaks_info:
width: int
start: datetime
until: datetime
tz: TzInfo | None
tz: tzinfo | None
42 changes: 25 additions & 17 deletions mizani/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
from .utils import get_null_value

if TYPE_CHECKING:
from typing import Any, Optional
from typing import Any

from mizani.typing import (
FloatArrayLike,
NDArrayFloat,
TFloatVector,
TupleFloat2,
TupleFloat4,
)


Expand All @@ -60,8 +58,8 @@

def rescale(
x: FloatArrayLike,
to: TupleFloat2 = (0, 1),
_from: Optional[TupleFloat2] = None,
to: tuple[float, float] = (0, 1),
_from: tuple[float, float] | None = None,
) -> NDArrayFloat:
"""
Rescale numeric vector to have specified minimum and maximum.
Expand Down Expand Up @@ -97,8 +95,8 @@ def rescale(

def rescale_mid(
x: FloatArrayLike,
to: TupleFloat2 = (0, 1),
_from: Optional[TupleFloat2] = None,
to: tuple[float, float] = (0, 1),
_from: tuple[float, float] | None = None,
mid: float = 0,
) -> NDArrayFloat:
"""
Expand Down Expand Up @@ -157,8 +155,8 @@ def rescale_mid(

def rescale_max(
x: FloatArrayLike,
to: TupleFloat2 = (0, 1),
_from: Optional[TupleFloat2] = None,
to: tuple[float, float] = (0, 1),
_from: tuple[float, float] | None = None,
) -> NDArrayFloat:
"""
Rescale numeric vector to have specified maximum.
Expand Down Expand Up @@ -224,7 +222,7 @@ def rescale_max(


def squish_infinite(
x: FloatArrayLike, range: TupleFloat2 = (0, 1)
x: FloatArrayLike, range: tuple[float, float] = (0, 1)
) -> NDArrayFloat:
"""
Truncate infinite values to a range.
Expand Down Expand Up @@ -261,7 +259,9 @@ def squish_infinite(


def squish(
x: FloatArrayLike, range: TupleFloat2 = (0, 1), only_finite: bool = True
x: FloatArrayLike,
range: tuple[float, float] = (0, 1),
only_finite: bool = True,
) -> NDArrayFloat:
"""
Squish values into range.
Expand Down Expand Up @@ -300,7 +300,7 @@ def squish(

def censor(
x: TFloatVector,
range: TupleFloat2 = (0, 1),
range: tuple[float, float] = (0, 1),
only_finite: bool = True,
) -> TFloatVector:
"""
Expand Down Expand Up @@ -438,8 +438,11 @@ def zero_range(x: tuple[Any, Any], tol: float = EPSILON * 100) -> bool:


def expand_range(
range: TupleFloat2, mul: float = 0, add: float = 0, zero_width: float = 1
) -> TupleFloat2:
range: tuple[float, float],
mul: float = 0,
add: float = 0,
zero_width: float = 1,
) -> tuple[float, float]:
"""
Expand a range with a multiplicative or additive constant
Expand Down Expand Up @@ -498,10 +501,15 @@ def expand_range(


def expand_range_distinct(
range: TupleFloat2,
expand: TupleFloat2 | TupleFloat4 = (0, 0, 0, 0),
range: tuple[float, float],
expand: (tuple[float, float] | tuple[float, float, float, float]) = (
0,
0,
0,
0,
),
zero_width: float = 1,
) -> TupleFloat2:
) -> tuple[float, float]:
"""
Expand a range with a multiplicative or additive constants
Expand Down
Loading

0 comments on commit a27e803

Please sign in to comment.