From 5ea5bd95d5bb93434fb5f1686f50b176c46dbac8 Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Tue, 8 Oct 2024 20:36:41 +0200 Subject: [PATCH] Typing: More precise NaT stub (#60002) * more precise NaT stub * ruff format * updated == and != to return literal --- pandas/_libs/tslibs/nattype.pyi | 40 +++++++++++++++------------------ 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index fa1577f033fff..d3b10fbe79cb9 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -25,12 +25,8 @@ NaT: NaTType iNaT: int nat_strings: set[str] -_NaTComparisonTypes: TypeAlias = ( - datetime | timedelta | Period | np.datetime64 | np.timedelta64 -) - -class _NatComparison: - def __call__(self, other: _NaTComparisonTypes) -> bool: ... +_TimeLike: TypeAlias = datetime | timedelta | Period | np.datetime64 | np.timedelta64 +_TimeDelta: TypeAlias = timedelta | np.timedelta64 class NaTType: _value: np.int64 @@ -161,30 +157,30 @@ class NaTType: @property def qyear(self) -> float: ... # comparisons - def __eq__(self, other: object) -> bool: ... - def __ne__(self, other: object) -> bool: ... - __lt__: _NatComparison - __le__: _NatComparison - __gt__: _NatComparison - __ge__: _NatComparison + def __eq__(self, other: object, /) -> Literal[False]: ... + def __ne__(self, other: object, /) -> Literal[True]: ... + def __lt__(self, other: Self | _TimeLike, /) -> Literal[False]: ... + def __le__(self, other: Self | _TimeLike, /) -> Literal[False]: ... + def __gt__(self, other: Self | _TimeLike, /) -> Literal[False]: ... + def __ge__(self, other: Self | _TimeLike, /) -> Literal[False]: ... # unary operators def __pos__(self) -> Self: ... def __neg__(self) -> Self: ... # binary operators - def __sub__(self, other: Self | timedelta | datetime) -> Self: ... - def __rsub__(self, other: Self | timedelta | datetime) -> Self: ... - def __add__(self, other: Self | timedelta | datetime) -> Self: ... - def __radd__(self, other: Self | timedelta | datetime) -> Self: ... - def __mul__(self, other: float) -> Self: ... # analogous to timedelta - def __rmul__(self, other: float) -> Self: ... + def __sub__(self, other: Self | _TimeLike, /) -> Self: ... + def __rsub__(self, other: Self | _TimeLike, /) -> Self: ... + def __add__(self, other: Self | _TimeLike, /) -> Self: ... + def __radd__(self, other: Self | _TimeLike, /) -> Self: ... + def __mul__(self, other: float, /) -> Self: ... # analogous to timedelta + def __rmul__(self, other: float, /) -> Self: ... @overload # analogous to timedelta - def __truediv__(self, other: Self | timedelta) -> float: ... # Literal[NaN] + def __truediv__(self, other: Self | _TimeDelta, /) -> float: ... # Literal[NaN] @overload - def __truediv__(self, other: float) -> Self: ... + def __truediv__(self, other: float, /) -> Self: ... @overload # analogous to timedelta - def __floordiv__(self, other: Self | timedelta) -> float: ... # Literal[NaN] + def __floordiv__(self, other: Self | _TimeDelta, /) -> float: ... # Literal[NaN] @overload - def __floordiv__(self, other: float) -> Self: ... + def __floordiv__(self, other: float, /) -> Self: ... # other def __hash__(self) -> int: ... def as_unit(self, unit: str, round_ok: bool = ...) -> NaTType: ...