Skip to content

add operators for Index #504

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 4 commits into from
Feb 3, 2023
Merged

add operators for Index #504

merged 4 commits into from
Feb 3, 2023

Conversation

Dr-Irv
Copy link
Collaborator

@Dr-Irv Dr-Irv commented Jan 9, 2023

Some notes:

  1. Ideally we would use OpsMixin, but we'd need to use return types of Self and mypy doesn't support that yet. I put a comment in arraylike.pyi about that.
  2. The idea here is to support the arithmetic operations on Index, where we don't know the dtype of the Index. Could eventually improve this if we ever make Index a Generic type.
  3. The boolean operators were there before, but they are now deprecated, so I removed them.

My philosophy is that this is "good enough for now", and if we get reports of issues on specific Index subclasses, we can then fix them going forward, rather than doing a complete set of tests now for all the Index subclasses.

@Dr-Irv Dr-Irv requested review from bashtage and twoertwein January 9, 2023 23:01
@Dr-Irv Dr-Irv added the Index Related to the Index class or subclasses label Jan 9, 2023
@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Jan 9, 2023

For this PR, would like BOTH @bashtage and @twoertwein to approve, and whomever does the second approval, can do the merge. Thanks.

@twoertwein
Copy link
Member

  1. Ideally we would use OpsMixin, but we'd need to use return types of Self and mypy doesn't support that yet. I put a comment in arraylike.pyi about that.

I think Self is just a short form to avoid TypeVars. Could define _OpsMixinT = TypeVar("_OpsMixinT", OpsMixin) and use it where you want to use Self? (haven't looked at the actual code changes.)

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Jan 10, 2023

Based on discussion here: python/mypy#14424 I changed the way we detect invalid Index with logical operations in 3b3f280

check(assert_type(i1 % i2, pd.Index), pd.Index)
check(assert_type(i1 % 10, pd.Index), pd.Index)
check(assert_type(10 % i1, pd.Index), pd.Index)
check(assert_type(divmod(i1, i2), Tuple[pd.Index, pd.Index]), tuple)
Copy link
Member

Choose a reason for hiding this comment

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

Not needed but could also check the tuple elements: check(..., tuple, pd.Index)

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Jan 11, 2023

@bashtage awaiting your approval/merge

@Dr-Irv Dr-Irv mentioned this pull request Jan 11, 2023
2 tasks
@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Jan 31, 2023

@bashtage can you review?

@Dr-Irv
Copy link
Collaborator Author

Dr-Irv commented Feb 3, 2023

@twoertwein haven't heard from @bashtage in a few weeks so can you merge it? Thanks.

i1 | i2, # type:ignore[operator] # pyright: ignore[reportGeneralTypeIssues]
Never,
)
assert_type( # type: ignore[assert-type]
Copy link
Member

Choose a reason for hiding this comment

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

Some of the checks have an ignore for assert_type and some don't. Why do some need an ignore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It has to do with how mypy processes Never with certain operators and arguments. If you look carefully, you'll see that the # ignore[assert-type] is occurring on any operator that has a numerical operand. Without the ignore, mypy says the assert_type() is wrong, thinking the result is Any. Probably some kind of mypy error, although the interpretation of the Never result is ambiguous.

@twoertwein twoertwein merged commit 9faaf6c into pandas-dev:main Feb 3, 2023
@twoertwein
Copy link
Member

Thanks @Dr-Irv

@Dr-Irv Dr-Irv deleted the issue405 branch February 4, 2023 17:03
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this pull request Apr 1, 2023
* add operators for Index

* Fix OpsMixin using TypeVar

* detect Never as argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Various Index operators missing
2 participants