Skip to content

TYP: more misc annotations #56675

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 29, 2023
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
10 changes: 5 additions & 5 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
from collections.abc import (
Generator,
Iterable,
Sequence,
)


Expand Down Expand Up @@ -853,7 +854,7 @@ def inner(x) -> None:
return inner


def is_instance_factory(_type) -> Callable[[Any], None]:
def is_instance_factory(_type: type | tuple[type, ...]) -> Callable[[Any], None]:
"""

Parameters
Expand All @@ -866,8 +867,7 @@ def is_instance_factory(_type) -> Callable[[Any], None]:
ValueError if x is not an instance of `_type`

"""
if isinstance(_type, (tuple, list)):
_type = tuple(_type)
if isinstance(_type, tuple):
type_repr = "|".join(map(str, _type))
else:
type_repr = f"'{_type}'"
Expand All @@ -879,7 +879,7 @@ def inner(x) -> None:
return inner


def is_one_of_factory(legal_values) -> Callable[[Any], None]:
def is_one_of_factory(legal_values: Sequence) -> Callable[[Any], None]:
callables = [c for c in legal_values if callable(c)]
legal_values = [c for c in legal_values if not callable(c)]

Expand Down Expand Up @@ -930,7 +930,7 @@ def is_nonnegative_int(value: object) -> None:
is_text = is_instance_factory((str, bytes))


def is_callable(obj) -> bool:
def is_callable(obj: object) -> bool:
"""

Parameters
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def asfreq(self, freq=None, how: str = "E") -> Self:
# ------------------------------------------------------------------
# Rendering Methods

def _formatter(self, boxed: bool = False):
def _formatter(self, boxed: bool = False) -> Callable[[object], str]:
if boxed:
return str
return "'{}'".format
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ def sequence_to_td64ns(
return data, inferred_freq


def _ints_to_td64ns(data, unit: str = "ns"):
def _ints_to_td64ns(data, unit: str = "ns") -> tuple[np.ndarray, bool]:
"""
Convert an ndarray with integer-dtype to timedelta64[ns] dtype, treating
the integers as multiples of the given timedelta unit.
Expand Down Expand Up @@ -1120,7 +1120,9 @@ def _ints_to_td64ns(data, unit: str = "ns"):
return data, copy_made


def _objects_to_td64ns(data, unit=None, errors: DateTimeErrorChoices = "raise"):
def _objects_to_td64ns(
data, unit=None, errors: DateTimeErrorChoices = "raise"
) -> np.ndarray:
"""
Convert a object-dtyped or string-dtyped array into an
timedelta64[ns]-dtyped array.
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def is_terminal() -> bool:
"min_rows",
10,
pc_min_rows_doc,
validator=is_instance_factory([type(None), int]),
validator=is_instance_factory((type(None), int)),
)
cf.register_option("max_categories", 8, pc_max_categories_doc, validator=is_int)

Expand Down Expand Up @@ -369,7 +369,7 @@ def is_terminal() -> bool:
cf.register_option("chop_threshold", None, pc_chop_threshold_doc)
cf.register_option("max_seq_items", 100, pc_max_seq_items)
cf.register_option(
"width", 80, pc_width_doc, validator=is_instance_factory([type(None), int])
"width", 80, pc_width_doc, validator=is_instance_factory((type(None), int))
)
cf.register_option(
"memory_usage",
Expand Down Expand Up @@ -850,14 +850,14 @@ def register_converter_cb(key) -> None:
"format.thousands",
None,
styler_thousands,
validator=is_instance_factory([type(None), str]),
validator=is_instance_factory((type(None), str)),
)

cf.register_option(
"format.na_rep",
None,
styler_na_rep,
validator=is_instance_factory([type(None), str]),
validator=is_instance_factory((type(None), str)),
)

cf.register_option(
Expand All @@ -867,11 +867,15 @@ def register_converter_cb(key) -> None:
validator=is_one_of_factory([None, "html", "latex", "latex-math"]),
)

# error: Argument 1 to "is_instance_factory" has incompatible type "tuple[
# ..., <typing special form>, ...]"; expected "type | tuple[type, ...]"
cf.register_option(
"format.formatter",
None,
styler_formatter,
validator=is_instance_factory([type(None), dict, Callable, str]),
validator=is_instance_factory(
(type(None), dict, Callable, str) # type: ignore[arg-type]
Copy link
Member Author

Choose a reason for hiding this comment

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

mypy is confused by Callable

),
)

cf.register_option("html.mathjax", True, styler_mathjax, validator=is_bool)
Expand All @@ -898,7 +902,7 @@ def register_converter_cb(key) -> None:
"latex.environment",
None,
styler_environment,
validator=is_instance_factory([type(None), str]),
validator=is_instance_factory((type(None), str)),
)


Expand Down
6 changes: 4 additions & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,13 @@ def _array_equivalent_float(left: np.ndarray, right: np.ndarray) -> bool:
return bool(((left == right) | (np.isnan(left) & np.isnan(right))).all())


def _array_equivalent_datetimelike(left: np.ndarray, right: np.ndarray):
def _array_equivalent_datetimelike(left: np.ndarray, right: np.ndarray) -> bool:
return np.array_equal(left.view("i8"), right.view("i8"))


def _array_equivalent_object(left: np.ndarray, right: np.ndarray, strict_nan: bool):
def _array_equivalent_object(
left: np.ndarray, right: np.ndarray, strict_nan: bool
) -> bool:
left = ensure_object(left)
right = ensure_object(right)

Expand Down
25 changes: 13 additions & 12 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
IndexLabel,
JoinValidate,
Level,
ListLike,
MergeHow,
MergeValidate,
MutableMappingT,
Expand Down Expand Up @@ -5349,11 +5350,11 @@ def reindex(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level = ...,
inplace: Literal[True],
errors: IgnoreRaise = ...,
Expand All @@ -5363,11 +5364,11 @@ def drop(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level = ...,
inplace: Literal[False] = ...,
errors: IgnoreRaise = ...,
Expand All @@ -5377,11 +5378,11 @@ def drop(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level = ...,
inplace: bool = ...,
errors: IgnoreRaise = ...,
Expand All @@ -5390,11 +5391,11 @@ def drop(

def drop(
self,
labels: IndexLabel | None = None,
labels: IndexLabel | ListLike = None,
*,
axis: Axis = 0,
index: IndexLabel | None = None,
columns: IndexLabel | None = None,
index: IndexLabel | ListLike = None,
columns: IndexLabel | ListLike = None,
level: Level | None = None,
inplace: bool = False,
errors: IgnoreRaise = "raise",
Expand Down
25 changes: 13 additions & 12 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
IntervalClosedType,
JSONSerializable,
Level,
ListLike,
Manager,
NaPosition,
NDFrameT,
Expand Down Expand Up @@ -4709,11 +4710,11 @@ def reindex_like(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level | None = ...,
inplace: Literal[True],
errors: IgnoreRaise = ...,
Expand All @@ -4723,11 +4724,11 @@ def drop(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level | None = ...,
inplace: Literal[False] = ...,
errors: IgnoreRaise = ...,
Expand All @@ -4737,11 +4738,11 @@ def drop(
@overload
def drop(
self,
labels: IndexLabel = ...,
labels: IndexLabel | ListLike = ...,
*,
axis: Axis = ...,
index: IndexLabel = ...,
columns: IndexLabel = ...,
index: IndexLabel | ListLike = ...,
columns: IndexLabel | ListLike = ...,
level: Level | None = ...,
inplace: bool_t = ...,
errors: IgnoreRaise = ...,
Expand All @@ -4750,11 +4751,11 @@ def drop(

def drop(
self,
labels: IndexLabel | None = None,
labels: IndexLabel | ListLike = None,
*,
axis: Axis = 0,
index: IndexLabel | None = None,
columns: IndexLabel | None = None,
index: IndexLabel | ListLike = None,
columns: IndexLabel | ListLike = None,
level: Level | None = None,
inplace: bool_t = False,
errors: IgnoreRaise = "raise",
Expand Down
24 changes: 17 additions & 7 deletions pandas/core/methods/selectn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from typing import (
TYPE_CHECKING,
Generic,
cast,
final,
)
Expand All @@ -32,32 +33,41 @@
from pandas._typing import (
DtypeObj,
IndexLabel,
NDFrameT,
)

from pandas import (
DataFrame,
Series,
)
else:
# Generic[...] requires a non-str, provide it with a plain TypeVar at
# runtime to avoid circular imports
from pandas._typing import T

NDFrameT = T
DataFrame = T
Series = T

class SelectN:
def __init__(self, obj, n: int, keep: str) -> None:

class SelectN(Generic[NDFrameT]):
def __init__(self, obj: NDFrameT, n: int, keep: str) -> None:
self.obj = obj
self.n = n
self.keep = keep

if self.keep not in ("first", "last", "all"):
raise ValueError('keep must be either "first", "last" or "all"')

def compute(self, method: str) -> DataFrame | Series:
def compute(self, method: str) -> NDFrameT:
raise NotImplementedError

@final
def nlargest(self):
def nlargest(self) -> NDFrameT:
return self.compute("nlargest")

@final
def nsmallest(self):
def nsmallest(self) -> NDFrameT:
return self.compute("nsmallest")

@final
Expand All @@ -72,7 +82,7 @@ def is_valid_dtype_n_method(dtype: DtypeObj) -> bool:
return needs_i8_conversion(dtype)


class SelectNSeries(SelectN):
class SelectNSeries(SelectN[Series]):
"""
Implement n largest/smallest for Series

Expand Down Expand Up @@ -163,7 +173,7 @@ def compute(self, method: str) -> Series:
return concat([dropped.iloc[inds], nan_index]).iloc[:findex]


class SelectNFrame(SelectN):
class SelectNFrame(SelectN[DataFrame]):
"""
Implement n largest/smallest for DataFrame

Expand Down
5 changes: 1 addition & 4 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,11 @@ def _pad_2d(
values: np.ndarray,
limit: int | None = None,
mask: npt.NDArray[np.bool_] | None = None,
):
) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
mask = _fillna_prep(values, mask)

if values.size:
algos.pad_2d_inplace(values, mask, limit=limit)
else:
# for test coverage
pass
return values, mask


Expand Down
Loading