Skip to content

Commit 64e978c

Browse files
authored
feat: use ruff to drive typing of submodules (#1448)
* classify ann * mypy * fix(comment): #1445 (comment)
1 parent 72138f7 commit 64e978c

File tree

10 files changed

+89
-39
lines changed

10 files changed

+89
-39
lines changed

pandas-stubs/_testing/__init__.pyi

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ def assert_timedelta_array_equal(
9292
obj: str = "TimedeltaArray",
9393
check_freq: bool = True,
9494
) -> None: ...
95-
def assert_numpy_array_equal(
96-
left,
97-
right,
98-
strict_nan: bool = False,
99-
check_dtype: bool | Literal["equiv"] = True,
100-
err_msg: str | None = None,
101-
check_same: Literal["copy", "same"] | None = None,
102-
obj: str = "numpy array",
103-
index_values: Index | np.ndarray | None = None,
104-
) -> None: ...
10595
def assert_extension_array_equal(
10696
left: ExtensionArray,
10797
right: ExtensionArray,
@@ -173,7 +163,6 @@ def assert_frame_equal(
173163
atol: float = 1e-8,
174164
obj: str = "DataFrame",
175165
) -> None: ...
176-
def assert_equal(left, right, **kwargs: Any) -> None: ...
177166
def assert_sp_array_equal(left: SparseArray, right: SparseArray) -> None: ...
178167
def assert_contains_all(iterable: Iterable[T], dic: Container[T]) -> None: ...
179168
def assert_copy(iter1: Iterable[T], iter2: Iterable[T], **eql_kwargs: Any) -> None: ...

pandas-stubs/core/algorithms.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import (
2+
Any,
23
Literal,
34
overload,
45
)
@@ -72,9 +73,9 @@ def value_counts(
7273
dropna: bool = True,
7374
) -> Series: ...
7475
def take(
75-
arr,
76+
arr: np.ndarray | ExtensionArray | Index | Series,
7677
indices: TakeIndexer,
7778
axis: Literal[0, 1] = 0,
7879
allow_fill: bool = False,
79-
fill_value=None,
80-
): ...
80+
fill_value: Any = None,
81+
) -> np_1darray | ExtensionArray: ...

pandas-stubs/core/groupby/groupby.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class GroupBy(BaseGroupBy[NDFrameT]):
302302
periods: int | Sequence[int] = 1,
303303
freq: Frequency | None = ...,
304304
axis: Axis | _NoDefaultDoNotUse = 0,
305-
fill_value=...,
305+
fill_value: Scalar | None = None,
306306
suffix: str | None = ...,
307307
) -> NDFrameT: ...
308308
@final

pandas-stubs/core/groupby/ops.pyi

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class BaseGrouper:
3939
def get_iterator(
4040
self, data: NDFrameT, axis: AxisInt = ...
4141
) -> Iterator[tuple[Hashable, NDFrameT]]: ...
42-
@final
43-
@cache_readonly
44-
def group_keys_seq(self): ...
4542
@cache_readonly
4643
def indices(self) -> dict[Hashable, npt.NDArray[np.intp]]: ...
4744
@final

pandas-stubs/core/interchange/dataframe_protocol.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ from typing import (
1414
cast,
1515
)
1616

17+
from typing_extensions import Self
18+
1719
class DlpackDeviceType(enum.IntEnum):
1820
CPU = cast(int, ...)
1921
CUDA = cast(int, ...)
@@ -58,7 +60,7 @@ class Buffer(ABC, metaclass=abc.ABCMeta):
5860
@abstractmethod
5961
def ptr(self) -> int: ...
6062
@abstractmethod
61-
def __dlpack__(self): ...
63+
def __dlpack__(self) -> Any: ...
6264
@abstractmethod
6365
def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]: ...
6466

@@ -94,7 +96,9 @@ class Column(ABC, metaclass=abc.ABCMeta):
9496
class DataFrame(ABC, metaclass=abc.ABCMeta):
9597
version: int
9698
@abstractmethod
97-
def __dataframe__(self, nan_as_null: bool = ..., allow_copy: bool = ...): ...
99+
def __dataframe__(
100+
self, nan_as_null: bool = ..., allow_copy: bool = ...
101+
) -> Self: ...
98102
@property
99103
@abstractmethod
100104
def metadata(self) -> dict[str, Any]: ...

pandas-stubs/core/resample.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Resampler(BaseGroupBy[NDFrameT]):
175175
# attributes via setattr
176176
class _GroupByMixin(Resampler[NDFrameT]):
177177
key: str | list[str] | None
178-
def __getitem__(self, key) -> Self: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
178+
def __getitem__(self, key: str | list[str] | None) -> Self: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
179179

180180
class DatetimeIndexResampler(Resampler[NDFrameT]): ...
181181

pandas-stubs/core/reshape/util.pyi

Lines changed: 0 additions & 1 deletion
This file was deleted.

pandas-stubs/errors/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MergeError(ValueError): ...
2020
class AccessorRegistrationWarning(Warning): ...
2121

2222
class AbstractMethodError(NotImplementedError):
23-
def __init__(self, class_instance, methodtype: str = ...) -> None: ...
23+
def __init__(self, class_instance: type, methodtype: str = ...) -> None: ...
2424

2525
class NumbaUtilError(Exception): ...
2626
class DuplicateLabelError(ValueError): ...
@@ -34,7 +34,7 @@ class IndexingError(Exception): ...
3434
class PyperclipException(RuntimeError): ...
3535

3636
class PyperclipWindowsException(PyperclipException):
37-
def __init__(self, message) -> None: ...
37+
def __init__(self, message: str) -> None: ...
3838

3939
class CSSWarning(UserWarning): ...
4040
class PossibleDataLossError(Exception): ...

pyproject.toml

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,60 @@ ignore = [
201201
"A004", # https://docs.astral.sh/ruff/rules/builtin-import-shadowing/
202202
"PYI001", # https://docs.astral.sh/ruff/rules/unprefixed-type-param/
203203
"PYI042", # https://docs.astral.sh/ruff/rules/snake-case-type-alias/
204-
"ERA001", "ANN001", "ANN201", "ANN204", "PLR0402",
205-
]
204+
"ERA001", "PLR0402", "PLC0105"
205+
]
206+
"*range.pyi" = [
207+
# TODO: remove when pandas-dev/pandas-stubs#1442 is resolved
208+
"ANN001", "ANN201", "ANN204", "ANN206",
209+
]
210+
"*category.pyi" = [
211+
# TODO: remove when pandas-dev/pandas-stubs#1443 is resolved
212+
"ANN001", "ANN201", "ANN204", "ANN206",
213+
]
214+
"*series.pyi" = [
215+
# TODO: remove when pandas-dev/pandas-stubs#1444 is resolved
216+
"ANN001", "ANN201", "ANN204", "ANN206",
217+
]
218+
"*frame.pyi" = [
219+
# TODO: remove when pandas-dev/pandas-stubs#1446 is resolved
220+
"ANN001", "ANN201", "ANN204", "ANN206",
221+
]
222+
"*index*" = [
223+
# TODO: remove when indexes are fully typed
224+
"ANN001", "ANN201", "ANN204", "ANN206",
225+
]
226+
"*computation*" = [
227+
# TODO: remove when computations are fully typed
228+
"ANN001", "ANN201", "ANN204", "ANN206",
229+
]
230+
"*plotting*" = [
231+
# TODO: remove when plottings are fully typed
232+
"ANN001", "ANN201", "ANN204", "ANN206",
233+
]
234+
"*io*" = [
235+
# TODO: remove when io is fully typed
236+
"ANN001", "ANN201", "ANN204", "ANN206",
237+
]
238+
"*window*" = [
239+
# TODO: remove when window is fully typed
240+
"ANN001", "ANN201", "ANN204", "ANN206",
241+
]
242+
"*generic.pyi" = [
243+
# TODO: remove when generic.pyi is fully typed
244+
"ANN001", "ANN201", "ANN204", "ANN206",
245+
]
246+
"*array*" = [
247+
# TODO: remove when array is fully typed
248+
"ANN001", "ANN201", "ANN204", "ANN206",
249+
]
250+
"*_libs*" = [
251+
# TODO: remove when _libs is fully typed
252+
"ANN001", "ANN201", "ANN204", "ANN206",
253+
]
254+
"*base.pyi" = [
255+
# TODO: remove when base.pyi's are fully typed
256+
"ANN001", "ANN201", "ANN204", "ANN206",
257+
]
206258
"scripts/*" = [
207259
# The following rules are ignored permanently for good reasons
208260
"EM", # https://docs.astral.sh/ruff/rules/#flake8-errmsg-em

tests/extension/decimal/array.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from collections.abc import (
55
Callable,
66
Iterable,
7+
MutableSequence,
8+
Sequence,
79
)
810
import decimal
911
import numbers
@@ -37,6 +39,7 @@
3739
from pandas._typing import (
3840
ArrayLike,
3941
AstypeArg,
42+
ListLike,
4043
ScalarIndexer,
4144
SequenceIndexer,
4245
SequenceNotStr,
@@ -91,7 +94,7 @@ class DecimalArray(OpsMixin, ExtensionScalarOpsMixin, ExtensionArray):
9194

9295
def __init__(
9396
self,
94-
values: list[decimal.Decimal | float] | np.ndarray,
97+
values: MutableSequence[decimal._DecimalNew] | np.ndarray | ExtensionArray,
9598
dtype: DecimalDtype | None = None,
9699
copy: bool = False,
97100
context: decimal.Context | None = None,
@@ -123,7 +126,7 @@ def dtype(self) -> DecimalDtype:
123126
@classmethod
124127
def _from_sequence(
125128
cls,
126-
scalars: list[decimal.Decimal | float] | np.ndarray,
129+
scalars: list[decimal._DecimalNew] | np.ndarray | ExtensionArray,
127130
dtype: DecimalDtype | None = None,
128131
copy: bool = False,
129132
) -> Self:
@@ -140,7 +143,9 @@ def _from_sequence_of_strings(
140143

141144
@classmethod
142145
def _from_factorized(
143-
cls, values: list[decimal.Decimal | float] | np.ndarray, original: Any
146+
cls,
147+
values: list[decimal._DecimalNew] | np.ndarray | ExtensionArray,
148+
original: Any,
144149
) -> Self:
145150
return cls(values)
146151

@@ -186,7 +191,7 @@ def reconstruct(
186191
x: (
187192
decimal.Decimal
188193
| numbers.Number
189-
| list[decimal.Decimal | float]
194+
| list[decimal._DecimalNew]
190195
| np.ndarray
191196
),
192197
) -> decimal.Decimal | numbers.Number | DecimalArray:
@@ -240,21 +245,24 @@ def astype(self, dtype, copy=True):
240245

241246
return super().astype(dtype, copy=copy)
242247

243-
def __setitem__(self, key: object, value: decimal._DecimalNew) -> None:
248+
def __setitem__(
249+
self,
250+
key: int | slice[Any, Any, Any] | ListLike,
251+
value: decimal._DecimalNew | Sequence[decimal._DecimalNew],
252+
) -> None:
244253
if is_list_like(value):
254+
assert isinstance(value, Iterable)
245255
if is_scalar(key):
246256
raise ValueError("setting an array element with a sequence.")
247-
value = [ # type: ignore[assignment]
248-
decimal.Decimal(v) # type: ignore[arg-type]
249-
for v in value # type: ignore[union-attr] # pyright: ignore[reportAssignmentType,reportGeneralTypeIssues]
257+
value = [
258+
decimal.Decimal(v) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
259+
for v in value
250260
]
251261
else:
252-
value = decimal.Decimal(value)
262+
value = decimal.Decimal(value) # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
253263

254-
key = check_array_indexer( # type: ignore[call-overload]
255-
self, key # pyright: ignore[reportArgumentType,reportCallIssue]
256-
)
257-
self._data[key] = value # type: ignore[call-overload] # pyright: ignore[reportArgumentType,reportCallIssue]
264+
key = check_array_indexer(self, key)
265+
self._data[key] = value
258266

259267
def __len__(self) -> int:
260268
return len(self._data)

0 commit comments

Comments
 (0)