Skip to content

Fix __getitem__() for various index types #537

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
Feb 16, 2023

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Feb 15, 2023

By making __getitem__() a mixin, it uncovered some odd hierarchy in the index types, where we had redundant declarations, and some incorrect detection of some invalid subtraction operations. So the changes here make the original tests pass, and then we can test __getitem__() on various index types.

Was able to get rid of a number of #type: ignore statements as a result.

Either @bashtage or @twoertwein can review/approve.

# type ignore needed because it doesn't like the type of self
@overload
def __getitem__( # type: ignore[misc]
self: IndexT,
Copy link
Member

Choose a reason for hiding this comment

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

I assume only Index inherits from this. Can you use _IndexGetitemMixinT for self/return value?

Copy link
Member

Choose a reason for hiding this comment

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

Could probably also start using typing.Self

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I assume only Index inherits from this. Can you use _IndexGetitemMixinT for self/return value?

Your assumption isn't correct. DatetimeIndex, PeriodIndex and TimedeltaIndex also inherit, and then are specifying the return type of __getitem__(x: int) via the Generic parameter.

Could probably also start using typing.Self

I tried creating a TypeVar as _IndexGetitemMixinT and it did work. Tried typing.Self, and it doesn't work. I think the type of self in this case has to be "bound" to the return type.

These changes are in commit 0db8a00

@twoertwein twoertwein merged commit 15e66d0 into pandas-dev:main Feb 16, 2023
@twoertwein
Copy link
Member

Thank you @Dr-Irv !

@Dr-Irv Dr-Irv deleted the issue536 branch February 27, 2023 22:18
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this pull request Apr 1, 2023
* Fix __getitem__() for various index types

* create TypeVar for the mixin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PeriodIndex element start_time and end_time attributes raise error
2 participants