Skip to content

Commit 320cf41

Browse files
GH1222 Candidate for ty on pandas-stubs (#1240)
* GH1222 Candidate for ty on pandas-stubs * GH1222 Fix pre-commit * Update pandas-stubs/core/strings.pyi Co-authored-by: Irv Lustig <[email protected]> * GH1222 PR feedback * GH1222 PR feedback --------- Co-authored-by: Irv Lustig <[email protected]>
1 parent ef87543 commit 320cf41

File tree

16 files changed

+142
-107
lines changed

16 files changed

+142
-107
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ jobs:
3535
- name: Run mypy on 'tests' (using the local stubs) and on the local stubs
3636
run: poetry run poe mypy
3737

38+
- name: Run ty on 'pandas-stubs' (using the local stubs) and on the local stubs
39+
run: poetry run poe ty
40+
3841
- name: Run pyright on 'tests' (using the local stubs) and on the local stubs
3942
run: poetry run poe pyright
4043

pandas-stubs/_typing.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ from typing import (
1919
SupportsIndex,
2020
TypedDict,
2121
TypeVar,
22+
Union,
2223
overload,
2324
)
2425

@@ -463,6 +464,10 @@ VoidDtypeArg: TypeAlias = (
463464
| Literal["V", "void", "void0"]
464465
)
465466

467+
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
468+
DtypeArg: TypeAlias = Dtype | Mapping[Hashable, Dtype]
469+
DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype
470+
466471
AstypeArg: TypeAlias = (
467472
BooleanDtypeArg
468473
| IntDtypeArg
@@ -479,10 +484,6 @@ AstypeArg: TypeAlias = (
479484
| DtypeObj
480485
)
481486

482-
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
483-
DtypeArg: TypeAlias = Dtype | Mapping[Hashable, Dtype]
484-
DtypeObj: TypeAlias = np.dtype[np.generic] | ExtensionDtype
485-
486487
# converters
487488
ConvertersArg: TypeAlias = Mapping[Hashable, Callable[[Dtype], Dtype]]
488489

@@ -513,7 +514,8 @@ IndexKeyFunc: TypeAlias = Callable[[Index], Index | AnyArrayLike] | None
513514

514515
# types of `func` kwarg for DataFrame.aggregate and Series.aggregate
515516
# More specific than what is in pandas
516-
AggFuncTypeBase: TypeAlias = Callable | str | np.ufunc
517+
# following Union is here to make it ty compliant https://github.com/astral-sh/ty/issues/591
518+
AggFuncTypeBase: TypeAlias = Union[Callable, str, np.ufunc] # noqa: UP007
517519
AggFuncTypeDictSeries: TypeAlias = Mapping[HashableT, AggFuncTypeBase]
518520
AggFuncTypeDictFrame: TypeAlias = Mapping[
519521
HashableT, AggFuncTypeBase | list[AggFuncTypeBase]

pandas-stubs/core/arrays/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import tzinfo
1+
from datetime import tzinfo as _tzinfo
22

33
import numpy as np
44
from pandas.core.arrays.datetimelike import (
@@ -28,7 +28,7 @@ class DatetimeArray(DatetimeLikeArrayMixin, TimelikeOps, DatelikeOps):
2828
@tz.setter
2929
def tz(self, value) -> None: ...
3030
@property
31-
def tzinfo(self) -> tzinfo | None: ...
31+
def tzinfo(self) -> _tzinfo | None: ...
3232
@property
3333
def is_normalized(self): ...
3434
def __array__(self, dtype=...) -> np.ndarray: ...

pandas-stubs/core/generic.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class NDFrame(indexing.IndexingMixin):
6969
def set_flags(
7070
self,
7171
*,
72-
copy: bool = ...,
73-
allows_duplicate_labels: bool | None = ...,
72+
copy: _bool = ...,
73+
allows_duplicate_labels: _bool | None = ...,
7474
) -> Self: ...
7575
@property
7676
def attrs(self) -> dict[Hashable | None, Any]: ...

pandas-stubs/core/groupby/groupby.pyi

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ from pandas._typing import (
7575

7676
from pandas.plotting import PlotAccessor
7777

78-
_GroupByT = TypeVar("_GroupByT", bound=GroupBy)
79-
8078
_KeysArgType: TypeAlias = (
8179
Hashable
8280
| list[Hashable]
@@ -91,67 +89,6 @@ _ResamplerGroupBy: TypeAlias = (
9189
| TimedeltaIndexResamplerGroupby[NDFrameT]
9290
)
9391

94-
# GroupByPlot does not really inherit from PlotAccessor but it delegates
95-
# to it using __call__ and __getattr__. We lie here to avoid repeating the
96-
# whole stub of PlotAccessor
97-
@final
98-
class GroupByPlot(PlotAccessor, Generic[_GroupByT]):
99-
def __init__(self, groupby: _GroupByT) -> None: ...
100-
# The following methods are inherited from the fake parent class PlotAccessor
101-
# def __call__(self, *args, **kwargs): ...
102-
# def __getattr__(self, name: str): ...
103-
104-
class BaseGroupBy(SelectionMixin[NDFrameT], GroupByIndexingMixin):
105-
axis: AxisInt
106-
grouper: ops.BaseGrouper
107-
keys: _KeysArgType | None
108-
level: IndexLabel | None
109-
group_keys: bool
110-
@final
111-
def __len__(self) -> int: ...
112-
@final
113-
def __repr__(self) -> str: ... # noqa: PYI029 __repr__ here is final
114-
@final
115-
@property
116-
def groups(self) -> dict[Hashable, Index]: ...
117-
@final
118-
@property
119-
def ngroups(self) -> int: ...
120-
@final
121-
@property
122-
def indices(self) -> dict[Hashable, Index | npt.NDArray[np.int_] | list[int]]: ...
123-
@overload
124-
def pipe(
125-
self,
126-
func: Callable[Concatenate[Self, P], T],
127-
*args: P.args,
128-
**kwargs: P.kwargs,
129-
) -> T: ...
130-
@overload
131-
def pipe(
132-
self,
133-
func: tuple[Callable[..., T], str],
134-
*args: Any,
135-
**kwargs: Any,
136-
) -> T: ...
137-
@final
138-
def get_group(self, name, obj: NDFrameT | None = ...) -> NDFrameT: ...
139-
@final
140-
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]: ...
141-
@overload
142-
def __getitem__(self: BaseGroupBy[DataFrame], key: Scalar) -> generic.SeriesGroupBy: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
143-
@overload
144-
def __getitem__(
145-
self: BaseGroupBy[DataFrame], key: Iterable[Hashable]
146-
) -> generic.DataFrameGroupBy: ...
147-
@overload
148-
def __getitem__(
149-
self: BaseGroupBy[Series[S1]],
150-
idx: list[str] | Index | Series[S1] | MaskType | tuple[Hashable | slice, ...],
151-
) -> generic.SeriesGroupBy: ...
152-
@overload
153-
def __getitem__(self: BaseGroupBy[Series[S1]], idx: Scalar) -> S1: ...
154-
15592
class GroupBy(BaseGroupBy[NDFrameT]):
15693
as_index: bool
15794
sort: bool
@@ -393,3 +330,66 @@ class GroupBy(BaseGroupBy[NDFrameT]):
393330
weights: Sequence | Series | None = ...,
394331
random_state: RandomState | None = ...,
395332
) -> NDFrameT: ...
333+
334+
_GroupByT = TypeVar("_GroupByT", bound=GroupBy)
335+
336+
# GroupByPlot does not really inherit from PlotAccessor but it delegates
337+
# to it using __call__ and __getattr__. We lie here to avoid repeating the
338+
# whole stub of PlotAccessor
339+
@final
340+
class GroupByPlot(PlotAccessor, Generic[_GroupByT]):
341+
def __init__(self, groupby: _GroupByT) -> None: ...
342+
# The following methods are inherited from the fake parent class PlotAccessor
343+
# def __call__(self, *args, **kwargs): ...
344+
# def __getattr__(self, name: str): ...
345+
346+
class BaseGroupBy(SelectionMixin[NDFrameT], GroupByIndexingMixin):
347+
axis: AxisInt
348+
grouper: ops.BaseGrouper
349+
keys: _KeysArgType | None
350+
level: IndexLabel | None
351+
group_keys: bool
352+
@final
353+
def __len__(self) -> int: ...
354+
@final
355+
def __repr__(self) -> str: ... # noqa: PYI029 __repr__ here is final
356+
@final
357+
@property
358+
def groups(self) -> dict[Hashable, Index]: ...
359+
@final
360+
@property
361+
def ngroups(self) -> int: ...
362+
@final
363+
@property
364+
def indices(self) -> dict[Hashable, Index | npt.NDArray[np.int_] | list[int]]: ...
365+
@overload
366+
def pipe(
367+
self,
368+
func: Callable[Concatenate[Self, P], T],
369+
*args: P.args,
370+
**kwargs: P.kwargs,
371+
) -> T: ...
372+
@overload
373+
def pipe(
374+
self,
375+
func: tuple[Callable[..., T], str],
376+
*args: Any,
377+
**kwargs: Any,
378+
) -> T: ...
379+
@final
380+
def get_group(self, name, obj: NDFrameT | None = ...) -> NDFrameT: ...
381+
@final
382+
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]: ...
383+
@overload
384+
def __getitem__(self: BaseGroupBy[DataFrame], key: Scalar) -> generic.SeriesGroupBy: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
385+
@overload
386+
def __getitem__(
387+
self: BaseGroupBy[DataFrame], key: Iterable[Hashable]
388+
) -> generic.DataFrameGroupBy: ...
389+
@overload
390+
def __getitem__(
391+
self: BaseGroupBy[Series[S1]],
392+
idx: list[str] | Index | Series[S1] | MaskType | tuple[Hashable | slice, ...],
393+
) -> generic.SeriesGroupBy: ...
394+
@overload
395+
def __getitem__(self: BaseGroupBy[Series[S1]], idx: Scalar) -> S1: ...

pandas-stubs/core/indexes/accessors.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime as dt
22
from datetime import (
33
timedelta,
4-
tzinfo,
4+
tzinfo as _tzinfo,
55
)
66
from typing import (
77
Generic,
@@ -119,7 +119,7 @@ class _FreqProperty(Generic[_DTFreqReturnType]):
119119

120120
class _TZProperty:
121121
@property
122-
def tz(self) -> tzinfo | None: ...
122+
def tz(self) -> _tzinfo | None: ...
123123

124124
class _DatetimeObjectOps(
125125
_FreqProperty[_DTFreqReturnType], _TZProperty, Generic[_DTFreqReturnType]
@@ -416,7 +416,7 @@ class DatetimeIndexProperties(
416416
@property
417417
def is_normalized(self) -> bool: ...
418418
@property
419-
def tzinfo(self) -> tzinfo | None: ...
419+
def tzinfo(self) -> _tzinfo | None: ...
420420
def to_pydatetime(self) -> npt.NDArray[np.object_]: ...
421421
def std(
422422
self, axis: int | None = ..., ddof: int = ..., skipna: bool = ...

pandas-stubs/core/indexes/base.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ class Index(IndexOpsMixin[S1]):
272272
Self,
273273
MultiIndex,
274274
np_ndarray_bool,
275-
Index[list[str]],
275+
Index[list[_str]],
276276
Index[int],
277277
Index[bytes],
278-
Index[str],
278+
Index[_str],
279279
Index[type[object]],
280280
]: ...
281281
def is_(self, other) -> bool: ...
@@ -478,7 +478,7 @@ class Index(IndexOpsMixin[S1]):
478478
UnknownIndex: TypeAlias = Index[Any]
479479

480480
def ensure_index_from_sequences(
481-
sequences: Sequence[Sequence[Dtype]], names: list[str] = ...
481+
sequences: Sequence[Sequence[Dtype]], names: list[_str] = ...
482482
) -> Index: ...
483483
def ensure_index(index_like: Sequence | Index, copy: bool = ...) -> Index: ...
484484
def maybe_extract_name(name, obj, cls) -> Label: ...

pandas-stubs/core/indexes/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from collections.abc import (
55
from datetime import (
66
datetime,
77
timedelta,
8-
tzinfo,
8+
tzinfo as _tzinfo,
99
)
1010
from typing import overload
1111

@@ -85,7 +85,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin[Timestamp], DatetimeIndexProperties):
8585
def to_julian_date(self) -> Index[float]: ...
8686
def isocalendar(self) -> DataFrame: ...
8787
@property
88-
def tzinfo(self) -> tzinfo | None: ...
88+
def tzinfo(self) -> _tzinfo | None: ...
8989
@property
9090
def dtype(self) -> np.dtype | DatetimeTZDtype: ...
9191
def shift(self, periods: int = ..., freq=...) -> Self: ...

0 commit comments

Comments
 (0)