Skip to content
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

Make IndexOpsMixin (and Index) generic #760

Merged
merged 25 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4929ecb
interpolate method
twoertwein Aug 2, 2023
2775a58
WIP
twoertwein Aug 2, 2023
9e40ddf
mypy handles Never differently - at least asssert that the method nev…
twoertwein Aug 3, 2023
68abe4d
use Self
twoertwein Aug 3, 2023
2cfbdb0
a few more S1/Self
twoertwein Aug 3, 2023
ebcc389
fix tests
twoertwein Aug 3, 2023
24d42fa
more tests
twoertwein Aug 3, 2023
f3802d3
add an ignore to let mypy pass
twoertwein Aug 3, 2023
92eab41
compat with old python versions
twoertwein Aug 3, 2023
6e04d8e
fix CategoricalIndex; MultiIndex should probably be Index[tuple[S1, .…
twoertwein Aug 3, 2023
e7a2d3b
Revert "interpolate method"
twoertwein Aug 3, 2023
d12a72a
many more overloads for subclasses (I wish pandas would not handle su…
twoertwein Aug 4, 2023
87c62d0
remove PandasObject - it caused the pyright issues but it provides no…
twoertwein Aug 4, 2023
9ce5fc4
works (except for np.ndarray)
twoertwein Aug 4, 2023
05b9c15
Merge remote-tracking branch 'upstream/main' into iter_interpolate
twoertwein Aug 8, 2023
d32f534
address the easy-to-fix comments
twoertwein Aug 8, 2023
4f13109
overloads for numpy
twoertwein Aug 9, 2023
bd39625
did numexpr break the CI?
twoertwein Aug 9, 2023
8c48567
lie: df.columns -> Index[str]
twoertwein Aug 9, 2023
67f5e8d
new ruff has far more pyi rules
twoertwein Aug 9, 2023
566c918
Make it clear that both mypy&pyright infer Never; but only pyright de…
twoertwein Aug 10, 2023
ff5ee58
use type aliases instead of npt
twoertwein Aug 10, 2023
c8d2019
fix issue with floordiv for mypy
Dr-Irv Aug 13, 2023
585c93b
fix comment in arraylike
Dr-Irv Aug 13, 2023
7ac1ea5
Merge pull request #3 from Dr-Irv/pr760
twoertwein Aug 14, 2023
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
18 changes: 7 additions & 11 deletions pandas-stubs/_libs/tslibs/timedeltas.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ import numpy as np
import pandas as pd
from pandas import (
DatetimeIndex,
Index,
PeriodIndex,
Series,
TimedeltaIndex,
)
from pandas.core.indexes.base import (
_FloatIndexType,
_IntIndexType,
)
from pandas.core.series import (
TimedeltaSeries,
TimestampSeries,
Expand Down Expand Up @@ -245,7 +242,7 @@ class Timedelta(timedelta):
@overload
def __mul__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __mul__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __mul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __rmul__(self, other: float) -> Timedelta: ...
@overload
Expand All @@ -254,8 +251,9 @@ class Timedelta(timedelta):
def __rmul__(self, other: Series[int]) -> TimedeltaSeries: ...
@overload
def __rmul__(self, other: Series[float]) -> TimedeltaSeries: ...
# maybe related to https://github.com/python/mypy/issues/10755
@overload
def __rmul__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __rmul__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ... # type: ignore[misc]
# Override due to more types supported than dt.timedelta
# error: Signature of "__floordiv__" incompatible with supertype "timedelta"
@overload # type: ignore[override]
Expand All @@ -271,9 +269,7 @@ class Timedelta(timedelta):
self, other: npt.NDArray[np.timedelta64]
) -> npt.NDArray[np.int_]: ...
@overload
def __floordiv__(
self, other: _IntIndexType | _FloatIndexType
) -> TimedeltaIndex: ...
def __floordiv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __floordiv__(self, other: Series[int]) -> TimedeltaSeries: ...
@overload
Expand Down Expand Up @@ -306,7 +302,7 @@ class Timedelta(timedelta):
@overload
def __truediv__(self, other: Series[float]) -> TimedeltaSeries: ...
@overload
def __truediv__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __truediv__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
def __rtruediv__(self, other: timedelta | Timedelta | NaTType) -> float: ...
# Override due to more types supported than dt.timedelta
@overload
Expand Down Expand Up @@ -338,7 +334,7 @@ class Timedelta(timedelta):
@overload
def __mod__(self, other: Series[int] | Series[float]) -> TimedeltaSeries: ...
@overload
def __mod__(self, other: _IntIndexType | _FloatIndexType) -> TimedeltaIndex: ...
def __mod__(self, other: Index[int] | Index[float]) -> TimedeltaIndex: ...
@overload
def __mod__(
self, other: npt.NDArray[np.integer] | npt.NDArray[np.floating]
Expand Down
29 changes: 7 additions & 22 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,12 @@ S1 = TypeVar(
| float
| complex
| Dtype
| Timestamp
| Timedelta
| datetime.datetime # includes pd.Timestamp
| datetime.timedelta # includes pd.Timedelta
| Period
| Interval[int]
| Interval[float]
| Interval[Timestamp]
| Interval[Timedelta]
| Interval[int | float | Timestamp | Timedelta]
| CategoricalDtype,
)
T1 = TypeVar(
"T1", str, int, np.int64, np.uint64, np.float64, float, np.dtype[np.generic]
)
T2 = TypeVar("T2", str, int)

IndexingInt: TypeAlias = (
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8
Expand Down Expand Up @@ -601,14 +594,9 @@ ByT = TypeVar(
| int
| float
| complex
| Timestamp
| Timedelta
| Scalar
| Period
| Interval[int]
| Interval[float]
| Interval[Timestamp]
| Interval[Timedelta]
| Interval[int | float | Timestamp | Timedelta]
| tuple,
)
# Use a distinct SeriesByT when using groupby with Series of known dtype.
Expand All @@ -622,13 +610,10 @@ SeriesByT = TypeVar(
| int
| float
| complex
| Timestamp
| Timedelta
| datetime.datetime
| datetime.timedelta
| Period
| Interval[int]
| Interval[float]
| Interval[Timestamp]
| Interval[Timedelta],
| Interval[int | float | Timestamp | Timedelta],
)
GroupByObjectNonScalar: TypeAlias = (
tuple
Expand Down
3 changes: 0 additions & 3 deletions pandas-stubs/core/accessor.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Any

class DirNamesMixin:
def __dir__(self): ...
Dr-Irv marked this conversation as resolved.
Show resolved Hide resolved

class PandasDelegate: ...

def delegate_names(
Expand Down
9 changes: 3 additions & 6 deletions pandas-stubs/core/arrays/categorical.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import numpy as np
from pandas import Series
from pandas.core.accessor import PandasDelegate as PandasDelegate
from pandas.core.arrays.base import ExtensionArray as ExtensionArray
from pandas.core.base import (
NoNewAttributesMixin as NoNewAttributesMixin,
PandasObject as PandasObject,
)
from pandas.core.base import NoNewAttributesMixin as NoNewAttributesMixin
from pandas.core.indexes.base import Index

from pandas._typing import (
Expand All @@ -33,7 +30,7 @@ from pandas.core.dtypes.dtypes import CategoricalDtype as CategoricalDtype

def contains(cat, key, container): ...

class Categorical(ExtensionArray, PandasObject):
class Categorical(ExtensionArray):
__array_priority__: int = ...
def __init__(
self,
Expand Down Expand Up @@ -185,7 +182,7 @@ class Categorical(ExtensionArray, PandasObject):
def repeat(self, repeats, axis=...): ...
def isin(self, values): ...

class CategoricalAccessor(PandasDelegate, PandasObject, NoNewAttributesMixin):
class CategoricalAccessor(PandasDelegate, NoNewAttributesMixin):
def __init__(self, data) -> None: ...
@property
def codes(self) -> Series[int]: ...
Expand Down
3 changes: 1 addition & 2 deletions pandas-stubs/core/arrays/sparse/array.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ from pandas.core.arrays import (
ExtensionArray,
ExtensionOpsMixin,
)
from pandas.core.base import PandasObject

from pandas._typing import TakeIndexer

class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin):
class SparseArray(ExtensionArray, ExtensionOpsMixin):
def __init__(
self,
data,
Expand Down
25 changes: 11 additions & 14 deletions pandas-stubs/core/base.pyi
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
from typing import (
Generic,
Iterator,
Literal,
)

import numpy as np
from pandas import Index
from pandas.core.accessor import DirNamesMixin
from pandas.core.arraylike import OpsMixin
from pandas.core.arrays import ExtensionArray
from pandas.core.arrays.categorical import Categorical
from typing_extensions import Self

from pandas._typing import (
S1,
AxisIndex,
NaPosition,
NDFrameT,
Scalar,
npt,
)

class PandasObject(DirNamesMixin):
def __sizeof__(self) -> int: ...

class NoNewAttributesMixin:
def __setattr__(self, key, value) -> None: ...

class SelectionMixin(Generic[NDFrameT]):
def ndim(self) -> int: ...
def __getitem__(self, key): ...

class IndexOpsMixin(OpsMixin):
class IndexOpsMixin(OpsMixin, Generic[S1]):
__array_priority__: int = ...
def transpose(self, *args, **kwargs) -> IndexOpsMixin: ...
def transpose(self, *args, **kwargs) -> Self: ...
@property
def T(self) -> IndexOpsMixin: ...
def T(self) -> Self: ...
@property
def shape(self) -> tuple: ...
@property
def ndim(self) -> int: ...
def item(self): ...
def item(self) -> S1: ...
@property
def nbytes(self) -> int: ...
@property
Expand All @@ -61,9 +60,9 @@ class IndexOpsMixin(OpsMixin):
def argmin(
self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs
) -> np.int64: ...
def tolist(self) -> list: ...
def to_list(self) -> list: ...
def __iter__(self): ...
def tolist(self) -> list[S1]: ...
def to_list(self) -> list[S1]: ...
def __iter__(self) -> Iterator[S1]: ...
@property
def hasnans(self) -> bool: ...
def value_counts(
Expand All @@ -87,6 +86,4 @@ class IndexOpsMixin(OpsMixin):
def searchsorted(
self, value, side: Literal["left", "right"] = ..., sorter=...
) -> int | list[int]: ...
def drop_duplicates(
self, *, keep: NaPosition | Literal[False] = ...
) -> IndexOpsMixin: ...
def drop_duplicates(self, *, keep: NaPosition | Literal[False] = ...) -> Self: ...
1 change: 0 additions & 1 deletion pandas-stubs/core/computation/ops.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Term:
def ndim(self) -> int: ...

class Constant(Term):
def __init__(self, value, env, side=..., encoding=...) -> None: ...
twoertwein marked this conversation as resolved.
Show resolved Hide resolved
@property
def name(self): ...

Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/computation/pytables.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Term(ops.Term):
def value(self, new_value) -> None: ...

class Constant(Term):
def __init__(self, value, env: PyTablesScope, side=..., encoding=...) -> None: ...
def __init__(self, name, env: PyTablesScope, side=..., encoding=...) -> None: ...

class BinOp(ops.BinOp):
op: str
Expand Down
3 changes: 1 addition & 2 deletions pandas-stubs/core/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ from typing import (

import numpy as np
from pandas import Index
from pandas.core.base import PandasObject
import pandas.core.indexing as indexing
from pandas.core.series import Series
import sqlalchemy.engine
Expand Down Expand Up @@ -54,7 +53,7 @@ from pandas.io.sql import SQLTable
_bool = bool
_str = str

class NDFrame(PandasObject, indexing.IndexingMixin):
class NDFrame(indexing.IndexingMixin):
__hash__: ClassVar[None] # type: ignore[assignment]

def set_flags(
Expand Down
9 changes: 3 additions & 6 deletions pandas-stubs/core/groupby/groupby.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ from collections.abc import (
)

import numpy as np
from pandas.core.base import (
PandasObject,
SelectionMixin,
)
from pandas.core.base import SelectionMixin
from pandas.core.frame import DataFrame
from pandas.core.generic import NDFrame
from pandas.core.groupby import ops
Expand All @@ -21,12 +18,12 @@ from pandas._typing import (
npt,
)

class GroupByPlot(PandasObject):
class GroupByPlot:
def __init__(self, groupby) -> None: ...
def __call__(self, *args, **kwargs): ...
def __getattr__(self, name: str): ...

class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT]):
class BaseGroupBy(SelectionMixin[NDFrameT]):
level = ...
as_index = ...
keys = ...
Expand Down
24 changes: 8 additions & 16 deletions pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ from pandas.core.arrays import (
DatetimeArray,
PeriodArray,
)
from pandas.core.base import (
NoNewAttributesMixin,
PandasObject,
)
from pandas.core.base import NoNewAttributesMixin
from pandas.core.frame import DataFrame
from pandas.core.indexes.base import _IntIndexType
from pandas.core.series import (
PeriodSeries,
Series,
Expand All @@ -40,10 +36,9 @@ from pandas._typing import (
np_ndarray_bool,
)

class Properties(PandasDelegate, PandasObject, NoNewAttributesMixin):
def __init__(self, data: Series, orig) -> None: ...
class Properties(PandasDelegate, NoNewAttributesMixin): ...

_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], _IntIndexType)
_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], Index[int])

class _DayLikeFieldOps(Generic[_DTFieldOpsReturnType]):
@property
Expand Down Expand Up @@ -302,7 +297,7 @@ class TimedeltaProperties(
): ...

_PeriodDTReturnTypes = TypeVar("_PeriodDTReturnTypes", TimestampSeries, DatetimeIndex)
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", Series[int], _IntIndexType)
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", Series[int], Index[int])
_PeriodStrReturnTypes = TypeVar("_PeriodStrReturnTypes", Series[str], Index)
_PeriodDTAReturnTypes = TypeVar("_PeriodDTAReturnTypes", DatetimeArray, DatetimeIndex)
_PeriodPAReturnTypes = TypeVar("_PeriodPAReturnTypes", PeriodArray, PeriodIndex)
Expand Down Expand Up @@ -335,8 +330,8 @@ class _PeriodProperties(
) -> _PeriodPAReturnTypes: ...

class PeriodIndexFieldOps(
_DayLikeFieldOps[_IntIndexType],
_PeriodProperties[DatetimeIndex, _IntIndexType, Index, DatetimeIndex, PeriodIndex],
_DayLikeFieldOps[Index[int]],
_PeriodProperties[DatetimeIndex, Index[int], Index, DatetimeIndex, PeriodIndex],
): ...
class PeriodProperties(
Properties,
Expand All @@ -347,7 +342,6 @@ class PeriodProperties(
_IsLeapYearProperty,
_FreqProperty[BaseOffset],
): ...

class CombinedDatetimelikeProperties(
DatetimeProperties[
Series[int],
Expand All @@ -362,9 +356,7 @@ class CombinedDatetimelikeProperties(
],
_TimedeltaPropertiesNoRounding[Series[int], Series[float]],
_PeriodProperties,
):
def __new__(cls, data: Series): ...

): ...
class TimestampProperties(
DatetimeProperties[
Series[int],
Expand All @@ -382,7 +374,7 @@ class TimestampProperties(
class DatetimeIndexProperties(
Properties,
_DatetimeNoTZProperties[
_IntIndexType,
Index[int],
np_ndarray_bool,
DatetimeIndex,
np.ndarray,
Expand Down
Loading