Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ repos:
types: [python]
stages: [manual]
additional_dependencies: &pyright_dependencies
- [email protected].383
- [email protected].404
- id: pyright
# note: assumes python env is setup and activated
name: pyright reportGeneralTypeIssues
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dependencies:

# code checks
- flake8=7.1.0 # run in subprocess over docstring examples
- mypy=1.13.0 # pre-commit uses locally installed mypy
- mypy=1.17.1 # pre-commit uses locally installed mypy
- tokenize-rt # scripts/check_for_inconsistent_pandas_namespace.py
- pre-commit>=4.2.0

Expand Down
77 changes: 40 additions & 37 deletions pandas/_libs/tslibs/dtypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,35 @@ class PeriodDtypeBase:
def _td64_unit(self) -> str: ...

class FreqGroup(Enum):
FR_ANN: int
FR_QTR: int
FR_MTH: int
FR_WK: int
FR_BUS: int
FR_DAY: int
FR_HR: int
FR_MIN: int
FR_SEC: int
FR_MS: int
FR_US: int
FR_NS: int
FR_UND: int
_value_: int
FR_ANN = ...
FR_QTR = ...
FR_MTH = ...
FR_WK = ...
FR_BUS = ...
FR_DAY = ...
FR_HR = ...
FR_MIN = ...
FR_SEC = ...
FR_MS = ...
FR_US = ...
FR_NS = ...
FR_UND = ...
@staticmethod
def from_period_dtype_code(code: int) -> FreqGroup: ...

class Resolution(Enum):
RESO_NS: int
RESO_US: int
RESO_MS: int
RESO_SEC: int
RESO_MIN: int
RESO_HR: int
RESO_DAY: int
RESO_MTH: int
RESO_QTR: int
RESO_YR: int
_value_: int
RESO_NS = ...
RESO_US = ...
RESO_MS = ...
RESO_SEC = ...
RESO_MIN = ...
RESO_HR = ...
RESO_DAY = ...
RESO_MTH = ...
RESO_QTR = ...
RESO_YR = ...
def __lt__(self, other: Resolution) -> bool: ...
def __ge__(self, other: Resolution) -> bool: ...
@property
Expand All @@ -67,17 +69,18 @@ class Resolution(Enum):
def attr_abbrev(self) -> str: ...

class NpyDatetimeUnit(Enum):
NPY_FR_Y: int
NPY_FR_M: int
NPY_FR_W: int
NPY_FR_D: int
NPY_FR_h: int
NPY_FR_m: int
NPY_FR_s: int
NPY_FR_ms: int
NPY_FR_us: int
NPY_FR_ns: int
NPY_FR_ps: int
NPY_FR_fs: int
NPY_FR_as: int
NPY_FR_GENERIC: int
_value_: int
NPY_FR_Y = ...
NPY_FR_M = ...
NPY_FR_W = ...
NPY_FR_D = ...
NPY_FR_h = ...
NPY_FR_m = ...
NPY_FR_s = ...
NPY_FR_ms = ...
NPY_FR_us = ...
NPY_FR_ns = ...
NPY_FR_ps = ...
NPY_FR_fs = ...
NPY_FR_as = ...
NPY_FR_GENERIC = ...
3 changes: 2 additions & 1 deletion pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ class SubclassedDataFrame(DataFrame):
def _constructor(self):
return lambda *args, **kwargs: SubclassedDataFrame(*args, **kwargs)

# error: Cannot override writeable attribute with read-only property
@property
def _constructor_sliced(self):
def _constructor_sliced(self): # type: ignore[override]
return lambda *args, **kwargs: SubclassedSeries(*args, **kwargs)


Expand Down
12 changes: 8 additions & 4 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ def round_trip_pathlib(writer, reader, path: str | None = None):
if path is None:
path = "___pathlib___"
with ensure_clean(path) as path:
writer(Path(path)) # type: ignore[arg-type]
obj = reader(Path(path)) # type: ignore[arg-type]
writer(Path(path))
obj = reader(Path(path))
return obj


def write_to_compressed(compression, path, data, dest: str = "test") -> None:
def write_to_compressed(compression, path: str, data, dest: str = "test") -> None:
"""
Write data to a compressed file.

Expand Down Expand Up @@ -138,5 +138,9 @@ def write_to_compressed(compression, path, data, dest: str = "test") -> None:
else:
raise ValueError(f"Unrecognized compression type: {compression}")

with compress_method(path, mode=mode) as f:
# error: No overload variant of "ZipFile" matches argument types "str", "str"
# error: No overload variant of "BZ2File" matches argument types "str", "str"
# error: Argument "mode" to "TarFile" has incompatible type "str";
# expected "Literal['r', 'a', 'w', 'x']
with compress_method(path, mode=mode) as f: # type: ignore[call-overload, arg-type]
getattr(f, method)(*args)
6 changes: 5 additions & 1 deletion pandas/core/_numba/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def column_looper(
else:

@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
def column_looper(
# error: Incompatible redefinition (redefinition with type
# "Callable[[ndarray[Any, Any], ndarray[Any, Any], ndarray[Any, Any],
# int, VarArg(Any)], Any]", original type "Callable[[ndarray[Any, Any],
# ndarray[Any, Any], int, int, VarArg(Any)], Any]")
def column_looper( # type: ignore[misc]
values: np.ndarray,
start: np.ndarray,
end: np.ndarray,
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _reconstruct_data(
values = cls._from_sequence(values, dtype=dtype) # type: ignore[assignment]

else:
values = values.astype(dtype, copy=False) # type: ignore[assignment]
values = values.astype(dtype, copy=False)

return values

Expand Down Expand Up @@ -904,7 +904,10 @@ def value_counts_internal(
.size()
)
result.index.names = values.names
counts = result._values
# error: Incompatible types in assignment (expression has type
# "ndarray[Any, Any] | DatetimeArray | TimedeltaArray | PeriodArray | Any",
# variable has type "ndarray[tuple[int, ...], dtype[Any]]")
counts = result._values # type: ignore[assignment]

else:
values = _ensure_arraylike(values, func_name="value_counts")
Expand Down Expand Up @@ -1311,7 +1314,7 @@ def searchsorted(
_diff_special = {"float64", "float32", "int64", "int32", "int16", "int8"}


def diff(arr, n: int, axis: AxisInt = 0):
def diff(arr, n: int | float | np.integer, axis: AxisInt = 0):
Copy link
Member

Choose a reason for hiding this comment

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

What about np.floating as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, added in next commit.

"""
difference of n between self,
analogous to s-s.shift(n)
Expand Down Expand Up @@ -1400,7 +1403,7 @@ def diff(arr, n: int, axis: AxisInt = 0):
if arr.dtype.name in _diff_special:
# TODO: can diff_2d dtype specialization troubles be fixed by defining
# out_arr inside diff_2d?
algos.diff_2d(arr, out_arr, n, axis, datetimelike=is_timedelta)
algos.diff_2d(arr, out_arr, int(n), axis, datetimelike=is_timedelta)
else:
# To keep mypy happy, _res_indexer is a list while res_indexer is
# a tuple, ditto for lag_indexer.
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ def agg_or_apply_dict_like(
assert op_name in ["agg", "apply"]

obj = self.obj
kwargs = {}
kwargs: dict[str, Any] = {}
if op_name == "apply":
by_row = "_compat" if self.by_row else False
kwargs.update({"by_row": by_row})
Expand Down Expand Up @@ -2012,7 +2012,8 @@ def _managle_lambda_list(aggfuncs: Sequence[Any]) -> Sequence[Any]:
for aggfunc in aggfuncs:
if com.get_callable_name(aggfunc) == "<lambda>":
aggfunc = partial(aggfunc)
aggfunc.__name__ = f"<lambda_{i}>"
# error: "partial[Any]" has no attribute "__name__"; maybe "__new__"?
aggfunc.__name__ = f"<lambda_{i}>" # type: ignore[attr-defined]
i += 1
mangled_aggfuncs.append(aggfunc)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/array_algos/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def _nanquantile(
# Caller is responsible for ensuring mask shape match
assert mask.shape == values.shape
result = [
_nanquantile_1d(val, m, qs, na_value, interpolation=interpolation) # type: ignore[arg-type]
_nanquantile_1d(val, m, qs, na_value, interpolation=interpolation)
for (val, m) in zip(list(values), list(mask))
]
if values.dtype.kind == "f":
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def method(self, *args, **kwargs):
return cast(F, method)


# error: Definition of "delete/ravel/T/repeat/copy" in base class "NDArrayBacked"
# is incompatible with definition in base class "ExtensionArray"
class NDArrayBackedExtensionArray(NDArrayBacked, ExtensionArray): # type: ignore[misc]
class NDArrayBackedExtensionArray(NDArrayBacked, ExtensionArray):
"""
ExtensionArray that is backed by a single NumPy ndarray.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def _coerce_to_array(
assert dtype == "boolean"
return coerce_to_array(value, copy=copy)

def _logical_method(self, other, op): # type: ignore[override]
def _logical_method(self, other, op):
assert op.__name__ in {"or_", "ror_", "and_", "rand_", "xor", "rxor"}
other_is_scalar = lib.is_scalar(other)
mask = None
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ def contains(cat, key, container) -> bool:
return any(loc_ in container for loc_ in loc)


# error: Definition of "delete/ravel/T/repeat/copy" in base class "NDArrayBacked"
# is incompatible with definition in base class "ExtensionArray"
class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin): # type: ignore[misc]
class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin):
"""
Represent a categorical variable in classic R / S-plus fashion.

Expand Down Expand Up @@ -2942,9 +2940,7 @@ def _validate(data) -> None:
def _delegate_property_get(self, name: str):
return getattr(self._parent, name)

# error: Signature of "_delegate_property_set" incompatible with supertype
# "PandasDelegate"
def _delegate_property_set(self, name: str, new_values) -> None: # type: ignore[override]
def _delegate_property_set(self, name: str, new_values) -> None:
setattr(self._parent, name, new_values)

@property
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ def new_meth(self, *args, **kwargs):
return cast(F, new_meth)


# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
# incompatible with definition in base class "ExtensionArray"
class DatetimeLikeArrayMixin( # type: ignore[misc]
OpsMixin, NDArrayBackedExtensionArray
):
class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
"""
Shared Base/Mixin class for DatetimeArray, TimedeltaArray, PeriodArray
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@

if TYPE_CHECKING:
from collections.abc import (
Callable,
Generator,
Iterator,
)

from pandas._typing import (
ArrayLike,
DateTimeErrorChoices,
DtypeObj,
IntervalClosedType,
TimeAmbiguous,
TimeNonexistent,
Expand Down Expand Up @@ -168,9 +170,7 @@ def f(self):
return property(f)


# error: Definition of "_concat_same_type" in base class "NDArrayBacked" is
# incompatible with definition in base class "ExtensionArray"
class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): # type: ignore[misc]
class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):
"""
Pandas ExtensionArray for tz-naive or tz-aware datetime data.

Expand Down Expand Up @@ -225,9 +225,9 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): # type: ignore[misc]
_typ = "datetimearray"
_internal_fill_value = np.datetime64("NaT", "ns")
_recognized_scalars = (datetime, np.datetime64)
_is_recognized_dtype = lambda x: lib.is_np_dtype(x, "M") or isinstance(
x, DatetimeTZDtype
)
_is_recognized_dtype: Callable[[DtypeObj], bool] = lambda x: lib.is_np_dtype(
x, "M"
) or isinstance(x, DatetimeTZDtype)
_infer_matches = ("datetime", "datetime64", "date")

@property
Expand Down Expand Up @@ -322,7 +322,7 @@ def _simple_new( # type: ignore[override]
else:
# DatetimeTZDtype. If we have e.g. DatetimeTZDtype[us, UTC],
# then values.dtype should be M8[us].
assert dtype._creso == get_unit_from_dtype(values.dtype) # type: ignore[union-attr]
assert dtype._creso == get_unit_from_dtype(values.dtype)

result = super()._simple_new(values, dtype)
result._freq = freq
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

from typing import ClassVar
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
)

import numpy as np

Expand All @@ -12,6 +16,9 @@
NumericDtype,
)

if TYPE_CHECKING:
from collections.abc import Callable


class FloatingDtype(NumericDtype):
"""
Expand All @@ -26,7 +33,7 @@ class FloatingDtype(NumericDtype):
# The value used to fill '_data' to avoid upcasting
_internal_fill_value = np.nan
_default_np_dtype = np.dtype(np.float64)
_checker = is_float_dtype
_checker: Callable[[Any], bool] = is_float_dtype

def construct_array_type(self) -> type[FloatingArray]:
"""
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

from typing import ClassVar
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
)

import numpy as np

Expand All @@ -12,6 +16,9 @@
NumericDtype,
)

if TYPE_CHECKING:
from collections.abc import Callable


class IntegerDtype(NumericDtype):
"""
Expand All @@ -26,7 +33,7 @@ class IntegerDtype(NumericDtype):
# The value used to fill '_data' to avoid upcasting
_internal_fill_value = 1
_default_np_dtype = np.dtype(np.int64)
_checker = is_integer_dtype
_checker: Callable[[Any], bool] = is_integer_dtype

def construct_array_type(self) -> type[IntegerArray]:
"""
Expand Down
Loading
Loading