Skip to content

GH1264 Version 2.0 cleanup #1265

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 2 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,16 @@ class Index(IndexOpsMixin[S1]):
def __neg__(self) -> Self: ...
def __nonzero__(self) -> None: ...
__bool__ = ...
def union(self, other: list[HashableT] | Index, sort=...) -> Index: ...
def intersection(self, other: list[S1] | Self, sort: bool = ...) -> Self: ...
def union(
self, other: list[HashableT] | Index, sort: bool | None = ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self, other: list[HashableT] | Index, sort: bool | None = ...
self, other: list[HashableT] | Self, sort: bool | None = ...

Might as well be consistent with these different operations

) -> Index: ...
def intersection(self, other: list[S1] | Self, sort: bool | None = ...) -> Self: ...
def difference(self, other: list | Index, sort: bool | None = None) -> Self: ...
def symmetric_difference(
self, other: list[S1] | Self, result_name: Hashable = ..., sort=...
self,
other: list[S1] | Self,
result_name: Hashable = ...,
sort: bool | None = ...,
) -> Self: ...
def get_loc(
self,
Expand Down Expand Up @@ -472,5 +477,6 @@ class Index(IndexOpsMixin[S1]):
| Sequence[float]
),
) -> Self: ...
def infer_objects(self, copy: bool = ...) -> Self: ...

UnknownIndex: TypeAlias = Index[Any]
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class MultiIndex(Index):
def equal_levels(self, other): ...
def union(self, other, sort=...): ... # pyrefly: ignore
def intersection( # pyright: ignore[reportIncompatibleMethodOverride]
self, other: list | Self, sort: bool = ...
self, other: list | Self, sort: bool | None = ...
): ...
def difference(self, other, sort=...): ...
def astype(self, dtype: DtypeArg, copy: bool = ...) -> Self: ...
Expand Down
7 changes: 7 additions & 0 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,13 @@ class TimedeltaSeries(Series[Timedelta]):
*args: Any,
**kwargs: Any,
) -> TimedeltaSeries: ...
def cumprod(
self,
axis: AxisIndex | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove None

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the default argument is None, wouldn't that cause issues?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I see what you mean, I was looking at the 2.3.0 code which still has None as default but on main it is now 0 (I would not see anyone use None by default and otherwise it will make it a good time to fix it).

skipna: _bool = ...,
*args: Any,
**kwargs: Any,
) -> Never: ...

class PeriodSeries(Series[Period]):
@property
Expand Down
25 changes: 25 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def test_difference_none() -> None:
# GH 253
check(assert_type(ind.difference([1]), "pd.Index[int]"), pd.Index)

# check with sort parameter
check(assert_type(ind.difference([1, None], sort=False), "pd.Index[int]"), pd.Index)
check(assert_type(ind.difference([1], sort=True), "pd.Index[int]"), pd.Index)


def test_str_split() -> None:
# GH 194
Expand Down Expand Up @@ -312,6 +316,20 @@ def test_range_index_union():
),
pd.Index,
)
check(
assert_type(
pd.RangeIndex(0, 10).union(["a", "b", "c"], sort=True),
Union[pd.Index, "pd.Index[int]", pd.RangeIndex],
),
pd.Index,
)
check(
assert_type(
pd.RangeIndex(0, 10).union(["a", "b", "c"], sort=False),
Union[pd.Index, "pd.Index[int]", pd.RangeIndex],
),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't seem right, and I think you'll need some specific overloads here for RangeIndex (and Sequence[int] for that matter). If RangeIndex has a set operation with either a Sequence[int], Series[int], Index[int] or RangeIndex, the result is Union[RangeIndex, Index[int]]. With any other type, it is just Index

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed that was the wrong copy paste, this is fixed.

pd.Index,
)


def test_range_index_start_stop_step():
Expand Down Expand Up @@ -1361,3 +1379,10 @@ def test_index_dict() -> None:
),
pd.TimedeltaIndex,
)


def test_index_infer_objects() -> None:
"""Test infer_objects method on Index."""
df = pd.DataFrame({"A": ["a", 1, 2, 3]})
idx = df.set_index("A").index[1:]
check(assert_type(idx.infer_objects(), pd.Index), pd.Index)
5 changes: 5 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3941,3 +3941,8 @@ def test_series_index_type() -> None:

if TYPE_CHECKING_INVALID_USAGE:
t = pd.Series([1, 2], index="ab") # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType]


def test_timedelta_index_cumsum() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def test_timedelta_index_cumsum() -> None:
def test_timedelta_invalid_cumprod() -> None:

if TYPE_CHECKING_INVALID_USAGE:
assert_type(pd.Series([pd.Timedelta(0), pd.Timedelta(1)]).cumprod(), Never)