Simplify and improve Series.sum#1362
Conversation
|
|
||
| from pandas.plotting import PlotAccessor | ||
|
|
||
| @type_check_only |
There was a problem hiding this comment.
I just realized that you added the use of type_check_only in a previous PR, but now I'm wondering if it makes sense in a stub file, because if you're using a stub file, doesn't that mean that its contents are only for type checking?
There was a problem hiding this comment.
This decorator is meant to be used with functions/classes that exist at type checking time but not at runtime. It is not strictly needed but it tells type checkers to flag any misuse of the decorated object at runtime. It is part of the typing module but it only exists at type checking time. The API documentation is here https://docs.python.org/3/library/typing.html#typing.type_check_only and the user guide is here https://typing.python.org/en/latest/guides/writing_stubs.html#stub-only-objects
There was a problem hiding this comment.
OK - thanks for the explanation. We probably should be using that more in the pandas stubs
assert_type()to assert the type of any return valueSeries.sumwas 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.sumdepends on the return value of its elements'__add__method. Fixing this simplifies the signature ofsumand 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_sumintest_series.py. Note also that the_SupportsAddtype-check-only protocol is added in the series module instead of_typingas it is very specific to this use case.