Simplify and improve Series.sum #1362
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
assert_type()
to assert the type of any return valueSeries.sum
was annotated to return the type of the series elements except forSeries[bool]
that was special-cased to returnint
. This is conceptually wrong, although it works for the most common series types. The return value ofSeries.sum
depends on the return value of its elements'__add__
method. Fixing this simplifies the signature ofsum
and makes it work generically (for example if in the futureSeries[np.bool]
becomes a thing).Note that I didn't add a test case for this as it is already tested in
test_types_sum
intest_series.py
. Note also that the_SupportsAdd
type-check-only protocol is added in the series module instead of_typing
as it is very specific to this use case.