-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Conversation
e331554
to
55d0bef
Compare
@overload | ||
def apply( # type: ignore[misc] | ||
self, func: Callable[[DataFrame], Series | Scalar], *args, **kwargs | ||
self, func: Callable[[Iterable], float], *args, **kwargs |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @danielroseman
assert_type()
to assert the type of any return valueThanks 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.