From 6915a826c434fcacb663bec553a15f6f19f3ea80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 7 Nov 2021 21:16:59 -0500 Subject: [PATCH 1/3] TYP: misc typing in _libs --- pandas/_libs/join.pyi | 12 ++++++------ pandas/_libs/ops.pyi | 9 +++++---- pandas/_libs/tslibs/fields.pyi | 4 ++-- pandas/_libs/tslibs/nattype.pyi | 18 ++++++++++++++++-- pandas/_libs/tslibs/period.pyi | 22 +++++++++++----------- pandas/_libs/tslibs/timedeltas.pyi | 13 ++++++++----- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/pandas/_libs/join.pyi b/pandas/_libs/join.pyi index 3a22aa439b7be..a5e91e2ce83eb 100644 --- a/pandas/_libs/join.pyi +++ b/pandas/_libs/join.pyi @@ -55,7 +55,7 @@ def asof_join_backward_on_X_by_Y( left_by_values: np.ndarray, # by_t[:] right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_forward_on_X_by_Y( left_values: np.ndarray, # asof_t[:] @@ -63,7 +63,7 @@ def asof_join_forward_on_X_by_Y( left_by_values: np.ndarray, # by_t[:] right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_nearest_on_X_by_Y( left_values: np.ndarray, # asof_t[:] @@ -71,23 +71,23 @@ def asof_join_nearest_on_X_by_Y( left_by_values: np.ndarray, # by_t[:] right_by_values: np.ndarray, # by_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_backward( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_forward( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... def asof_join_nearest( left_values: np.ndarray, # asof_t[:] right_values: np.ndarray, # asof_t[:] allow_exact_matches: bool = ..., - tolerance=..., + tolerance: np.number | int | float | None = ..., ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ... diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index 11ae3b852e97a..a0e40aca1450a 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -3,6 +3,7 @@ from typing import ( Callable, Literal, overload, + Iterable, ) import numpy as np @@ -35,15 +36,15 @@ def vec_binop( @overload def maybe_convert_bool( arr: npt.NDArray[np.object_], - true_values=..., - false_values=..., + true_values: Iterable = ..., + false_values: Iterable = ..., convert_to_masked_nullable: Literal[False] = ..., ) -> tuple[np.ndarray, None]: ... @overload def maybe_convert_bool( arr: npt.NDArray[np.object_], - true_values=..., - false_values=..., + true_values: Iterable = ..., + false_values: Iterable = ..., *, convert_to_masked_nullable: Literal[True], ) -> tuple[np.ndarray, np.ndarray]: ... diff --git a/pandas/_libs/tslibs/fields.pyi b/pandas/_libs/tslibs/fields.pyi index cbf91f2bcaf76..415b4329310c0 100644 --- a/pandas/_libs/tslibs/fields.pyi +++ b/pandas/_libs/tslibs/fields.pyi @@ -9,7 +9,7 @@ def month_position_check(fields, weekdays) -> str | None: ... def get_date_name_field( dtindex: npt.NDArray[np.int64], # const int64_t[:] field: str, - locale=..., + locale: str | None = ..., ) -> npt.NDArray[np.object_]: ... def get_start_end_field( dtindex: npt.NDArray[np.int64], # const int64_t[:] @@ -31,7 +31,7 @@ def isleapyear_arr( def build_isocalendar_sarray( dtindex: npt.NDArray[np.int64], # const int64_t[:] ) -> np.ndarray: ... -def get_locale_names(name_type: str, locale: object = ...): ... +def get_locale_names(name_type: str, locale: str | None = ...): ... class RoundTo: @property diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index 6a5555cfff030..2c555368359f3 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -1,6 +1,7 @@ from datetime import ( datetime, timedelta, + tzinfo as _tzinfo, ) from typing import Any @@ -17,7 +18,7 @@ class NaTType(datetime): def asm8(self) -> np.datetime64: ... def to_datetime64(self) -> np.datetime64: ... def to_numpy( - self, dtype=..., copy: bool = ... + self, dtype: np.dtype | None = ..., copy: bool = ... ) -> np.datetime64 | np.timedelta64: ... @property def is_leap_year(self) -> bool: ... @@ -69,7 +70,20 @@ class NaTType(datetime): def ceil(self) -> NaTType: ... def tz_convert(self) -> NaTType: ... def tz_localize(self) -> NaTType: ... - def replace(self, *args, **kwargs) -> NaTType: ... + # error: Signature of "replace" incompatible with supertype "datetime" + def replace( # type: ignore[override] + self, + year: int | None = ..., + month: int | None = ..., + day: int | None = ..., + hour: int | None = ..., + minute: int | None = ..., + second: int | None = ..., + microsecond: int | None = ..., + nanosecond: int | None = ..., + tzinfo: _tzinfo | None = ..., + fold: int | None = ..., + ) -> NaTType: ... # error: Return type "float" of "year" incompatible with return # type "int" in supertype "date" @property diff --git a/pandas/_libs/tslibs/period.pyi b/pandas/_libs/tslibs/period.pyi index 4f7505fd7e792..2f60df0ad888e 100644 --- a/pandas/_libs/tslibs/period.pyi +++ b/pandas/_libs/tslibs/period.pyi @@ -59,22 +59,22 @@ class Period: def __new__( # type: ignore[misc] cls, value=..., - freq=..., - ordinal=..., - year=..., - month=..., - quarter=..., - day=..., - hour=..., - minute=..., - second=..., + freq: int | str | None = ..., + ordinal: int | None = ..., + year: int | None = ..., + month: int | None = ..., + quarter: int | None = ..., + day: int | None = ..., + hour: int | None = ..., + minute: int | None = ..., + second: int | None = ..., ) -> Period | NaTType: ... @classmethod def _maybe_convert_freq(cls, freq) -> BaseOffset: ... @classmethod def _from_ordinal(cls, ordinal: int, freq) -> Period: ... @classmethod - def now(cls, freq=...) -> Period: ... + def now(cls, freq: BaseOffset = ...) -> Period: ... def strftime(self, fmt: str) -> str: ... def to_timestamp( self, @@ -82,7 +82,7 @@ class Period: how: str = ..., tz: Timezone | None = ..., ) -> Timestamp: ... - def asfreq(self, freq, how=...) -> Period: ... + def asfreq(self, freq: str, how: str = ...) -> Period: ... @property def freqstr(self) -> str: ... @property diff --git a/pandas/_libs/tslibs/timedeltas.pyi b/pandas/_libs/tslibs/timedeltas.pyi index 7c0131cf28c9a..d8369f0cc90f9 100644 --- a/pandas/_libs/tslibs/timedeltas.pyi +++ b/pandas/_libs/tslibs/timedeltas.pyi @@ -14,7 +14,7 @@ from pandas._libs.tslibs import ( ) from pandas._typing import npt -_S = TypeVar("_S") +_S = TypeVar("_S", bound=timedelta) def ints_to_pytimedelta( arr: npt.NDArray[np.int64], # const int64_t[:] @@ -36,7 +36,10 @@ class Timedelta(timedelta): # error: "__new__" must return a class instance (got "Union[Timedelta, NaTType]") def __new__( # type: ignore[misc] - cls: Type[_S], value=..., unit=..., **kwargs + cls: Type[_S], + value=..., + unit: str = ..., + **kwargs: int | float | np.integer | np.floating, ) -> _S | NaTType: ... @property def days(self) -> int: ... @@ -50,9 +53,9 @@ class Timedelta(timedelta): @property def asm8(self) -> np.timedelta64: ... # TODO: round/floor/ceil could return NaT? - def round(self: _S, freq) -> _S: ... - def floor(self: _S, freq) -> _S: ... - def ceil(self: _S, freq) -> _S: ... + def round(self: _S, freq: str) -> _S: ... + def floor(self: _S, freq: str) -> _S: ... + def ceil(self: _S, freq: str) -> _S: ... @property def resolution_string(self) -> str: ... def __add__(self, other: timedelta) -> timedelta: ... From 4a826f6b9a5eaf878e59c169e4c3cf73e42175f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 7 Nov 2021 23:18:18 -0500 Subject: [PATCH 2/3] isort --- pandas/_libs/ops.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/ops.pyi b/pandas/_libs/ops.pyi index a0e40aca1450a..74a6ad87cd279 100644 --- a/pandas/_libs/ops.pyi +++ b/pandas/_libs/ops.pyi @@ -1,9 +1,9 @@ from typing import ( Any, Callable, + Iterable, Literal, overload, - Iterable, ) import numpy as np From bab0fbd0efce5b7eb287a1f6317abd4a11a88f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 12 Dec 2021 14:39:08 -0500 Subject: [PATCH 3/3] to_numpy --- pandas/_libs/tslibs/nattype.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/nattype.pyi b/pandas/_libs/tslibs/nattype.pyi index 2c555368359f3..1e29ef8940891 100644 --- a/pandas/_libs/tslibs/nattype.pyi +++ b/pandas/_libs/tslibs/nattype.pyi @@ -18,7 +18,7 @@ class NaTType(datetime): def asm8(self) -> np.datetime64: ... def to_datetime64(self) -> np.datetime64: ... def to_numpy( - self, dtype: np.dtype | None = ..., copy: bool = ... + self, dtype: np.dtype | str | None = ..., copy: bool = ... ) -> np.datetime64 | np.timedelta64: ... @property def is_leap_year(self) -> bool: ...