Skip to content

TYP: misc typing in _libs #44349

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

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions pandas/_libs/join.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,39 @@ 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[:]
right_values: np.ndarray, # asof_t[:]
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[:]
right_values: np.ndarray, # asof_t[:]
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]]: ...
9 changes: 5 additions & 4 deletions pandas/_libs/ops.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import (
Any,
Callable,
Iterable,
Literal,
overload,
)
Expand Down Expand Up @@ -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]: ...
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions pandas/_libs/tslibs/nattype.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import (
datetime,
timedelta,
tzinfo as _tzinfo,
)
from typing import Any

Expand All @@ -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 | str | None = ..., copy: bool = ...
) -> np.datetime64 | np.timedelta64: ...
@property
def is_leap_year(self) -> bool: ...
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions pandas/_libs/tslibs/period.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,30 @@ 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,
freq: str | BaseOffset | None = ...,
how: str = ...,
tz: Timezone | None = ...,
) -> Timestamp: ...
def asfreq(self, freq, how=...) -> Period: ...
def asfreq(self, freq: str, how: str = ...) -> Period: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

freq can also be a BaseOffset but the documentation requires a str.

If the documentation is out of date, BaseOffset should be added. It might be that the type was intentionally restricted to make future refactoring/simplifications possible?

I'll only comment on these cases for now.

@property
def freqstr(self) -> str: ...
@property
Expand Down
13 changes: 8 additions & 5 deletions pandas/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]
Expand All @@ -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: ...
Expand All @@ -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: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also be BaseOffset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added most annotations based on the documentation (when available): it mentions only strings. freq ends up in to_offset so it should support BaseOffset.

@property
def resolution_string(self) -> str: ...
def __add__(self, other: timedelta) -> timedelta: ...
Expand Down