Skip to content

add Hashable type to __get_item__ #592 #596

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 5 commits into from
Mar 29, 2023
Merged

Conversation

anilbey
Copy link
Contributor

@anilbey anilbey commented Mar 29, 2023

SECOND = "haydar"

df = pd.DataFrame(data = [[12.2, 10], [8.8, 15]], columns=[MyEnum.FIRST, MyEnum.SECOND])
df[MyEnum.FIRST]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you use check(assert_type(df[MyEnum.FIRST], pd.Series), pd.Series)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks I just added it.

@@ -516,7 +516,7 @@ class DataFrame(NDFrame, OpsMixin):
def T(self) -> DataFrame: ...
def __getattr__(self, name: str) -> Series: ...
@overload
def __getitem__(self, idx: Scalar | tuple[Hashable, ...]) -> Series: ...
def __getitem__(self, idx: Scalar | Hashable | tuple[Hashable, ...]) -> Series: ...
Copy link
Member

Choose a reason for hiding this comment

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

Could reduce it to def __getitem__(self, idx: Scalar | Hashable) -> Series: ... as a tuple is declared as hashable in typeshed.

(Many members of Scalar are probably also hashable - not sure whether all of them are hashable.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks I just removed tuple[Hashable, ...] from the signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similarly, should we include a list[Hashable] in the 3rd overload?

The 3rd overload:

    @overload
    def __getitem__(
        self,
        idx: Series[_bool]
        | DataFrame
        | Index
        | np_ndarray_str
        | np_ndarray_bool
        | list[_ScalarOrTupleT],
    ) -> DataFrame: ...

Copy link
Member

Choose a reason for hiding this comment

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

Similarly, should we include a list[Hashable] in the 3rd overload?

Would need list[HashableT] to simulate the covariant behavior.

Do we have tests for __getitem__ and slice? I believe that should be failing now since slice is hashable. Probably would need to make the slice overload the first overload.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer a more incremental approach here. I'm concerned that we might be making some of the types too wide. Let's have this PR address the issue as reported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually more complicated than I thought.

Do we have tests for getitem and slice?

Yes, there is a slice test, but it is without assert types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just added more assert_types in the test to make sure the other overloads are still working fine.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

thanks @anilbey

@Dr-Irv Dr-Irv merged commit e3d9a1f into pandas-dev:main Mar 29, 2023
@anilbey
Copy link
Contributor Author

anilbey commented Mar 29, 2023

I'm happy to help, thanks @Dr-Irv , @twoertwein for the quick reply and the feedback.

twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this pull request Apr 1, 2023
* add Hashable type to __get_item__ pandas-dev#592

* add assert_type to test_types_getitem_with_hashable

* code style: formatting

* remove tuple[Hashable, ...] from get_item overload1

* added more assert_type to assure other overloads are ok
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.

BUG: __get_item__ accepts more types than it's type signature says
3 participants