Skip to content

More specific types for GroupBy.apply. #177

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 3 commits into from
Aug 3, 2022

Conversation

danielroseman
Copy link
Contributor

Thanks for the previous change (#168) on this issue, but unfortunately it didn't quite cover all the use cases - on my code base it frequently fell through to the union of DataFrame and Series, which would have required a lot of extra narrowing. This change is more specific and adds some extra test cases which should catch all the variants.

Also added a missing sum method on SeriesGroupBy.

@overload
def apply( # type: ignore[misc]
self, func: Callable[[DataFrame], Series | Scalar], *args, **kwargs
self, func: Callable[[Iterable], float], *args, **kwargs
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Callable[[Iterable]... should come last, because you want the other ones to match first. I think that will make pyright not see things as overlapping.

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 thought that originally. But then mypy gives the following error:

tests/test_frame.py:559: error: Incompatible types in assignment (expression has type "Series[Any]", variable has type "DataFrame")  [assignment]

I can't really explain that behaviour, because according to everything I can find mypy should also match in order.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I just tried moving it last and it worked. Still had pyright complain about the last overload never being matched without the #type: ignore in there, but it did work. Can you try again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Apologies, looks like I had the previous version of mypy. Running the reordered code against the latest version works correctly.

@overload
def apply( # type: ignore[misc]
self, func: Callable[[DataFrame], Series | Scalar], *args, **kwargs
self, func: Callable[[Iterable], float], *args, **kwargs
Copy link
Collaborator

Choose a reason for hiding this comment

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

I just tried moving it last and it worked. Still had pyright complain about the last overload never being matched without the #type: ignore in there, but it did work. Can you try again?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Aug 3, 2022

pyright reports an overloading problem due to what I think is a bug with strict type checking. Reported here: microsoft/pyright#3780

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.

@Dr-Irv Dr-Irv merged commit 02e1748 into pandas-dev:main Aug 3, 2022
@danielroseman danielroseman deleted the fix-groupby-apply branch August 8, 2022 09:41
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.

2 participants