Skip to content

Conversation

hamdanal
Copy link
Contributor

@hamdanal hamdanal commented Sep 8, 2025

  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

Series.sum was annotated to return the type of the series elements except for Series[bool] that was special-cased to return int. This is conceptually wrong, although it works for the most common series types. The return value of Series.sum depends on the return value of its elements' __add__ method. Fixing this simplifies the signature of sum and makes it work generically (for example if in the future Series[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 in test_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.


from pandas.plotting import PlotAccessor

@type_check_only
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 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?

Copy link
Contributor Author

@hamdanal hamdanal Sep 9, 2025

Choose a reason for hiding this comment

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

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK - thanks for the explanation. We probably should be using that more in the pandas stubs

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 @hamdanal

@cmp0xff you may want to take a look at this. It might inspire an idea to deal with the issues with TimestampSeries removal.

@Dr-Irv Dr-Irv merged commit cfed148 into pandas-dev:main Sep 10, 2025
13 checks passed
@hamdanal hamdanal deleted the sum branch September 10, 2025 08:56
@cmp0xff cmp0xff mentioned this pull request Sep 28, 2025
2 tasks
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