Skip to content

Conversation

@roman-y-wu
Copy link

  • Updated aggregation tests in test_agg.py to include checks for the var() method across different data types (float, bool, int, and complex).

  • Added a warning check for complex series when calculating variance to handle potential loss of imaginary parts.

  • Enhanced type assertions in test_series.py for the var() method to ensure it returns a float type.

  • Closes Scalar type to float error with pyright #1545 (Replace xxxx with the Github issue number)

  • Tests added (Please use assert_type() to assert the type of any return value)

  • If I used AI to develop this pull request, I prompted it to follow AGENTS.md.

- Updated aggregation tests in `test_agg.py` to include checks for the `var()` method across different data types (float, bool, int, and complex).
- Added a warning check for complex series when calculating variance to handle potential loss of imaginary parts.
- Enhanced type assertions in `test_series.py` for the `var()` method to ensure it returns a float type.
Copilot AI review requested due to automatic review settings December 13, 2025 07:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds comprehensive variance checks to aggregation tests and enhances type assertions for the var() method across different data types (float, bool, int, and complex). The PR addresses issue #1545 by ensuring the variance method has proper type stubs and test coverage.

  • Added type assertions using assert_type() and check() for var() method calls in type checking tests
  • Added variance test coverage across different data types with proper warning checks for complex numbers
  • Updated type stubs with overloaded signatures for the var() method to return appropriate types based on series dtype

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/series/test_series.py Enhanced test_types_var() with type assertions to verify var() returns float type
tests/series/test_agg.py Added var() checks across all aggregation tests for different data types (float, bool, int, complex, str, timedelta) with appropriate warning handling for complex series
tests/_typing.py SHOULD NOT BE COMMITTED - This is an auto-generated file during test runs per tests/conftest.py
pandas-stubs/core/series.pyi Added overloaded type signatures for var() method with specific return types for different series types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@cmp0xff cmp0xff left a comment

Choose a reason for hiding this comment

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

Since you have clicked If I used AI to develop this pull request, I prompted it to follow AGENTS.md., please also follow the section https://github.com/pandas-dev/pandas-stubs/blob/main/AGENTS.md#pull-requests-summary

ddof: int = 1,
numeric_only: _bool = False,
**kwargs: Any,
) -> np.float64: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> np.float64: ...
) -> float: ...

check(assert_type(series.mean(), pd.Timedelta), pd.Timedelta)
check(assert_type(series.median(), pd.Timedelta), pd.Timedelta)
check(assert_type(series.std(), pd.Timedelta), pd.Timedelta)
# Note: var() is not supported for Timedelta series at runtime
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use if TYPE_CHECKING_INVALID_USAGE and make sure series.var() here gives either errors by type checkers (like in test_agg_str) or Never (assert_type(series.var(), Never)).

Error is preferable over Never.


check(assert_type(series.mean(), pd.Timestamp), pd.Timestamp)
check(assert_type(series.median(), pd.Timestamp), pd.Timestamp)
check(assert_type(series.std(), pd.Timedelta), pd.Timedelta)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use if TYPE_CHECKING_INVALID_USAGE and make sure series.var() here gives either errors by type checkers (like in test_agg_str) or Never (assert_type(series.var(), Never)).

Error is preferable over Never.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add a check here for series.var()

@loicdiridollou
Copy link
Member

/pandas_nightly

- Fix `Series[complex].var()` return type from `np.float64` to `float`
- Add `Series[Timedelta].var()` overload returning `Never` (invalid operation)
- Add upper bound `upper="2.3.99"` for complex variance warning test
- Add Timedelta var() invalid usage test
Copy link
Contributor

@cmp0xff cmp0xff left a comment

Choose a reason for hiding this comment

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

Getting close.

check(assert_type(series.median(), pd.Timedelta), pd.Timedelta)
check(assert_type(series.std(), pd.Timedelta), pd.Timedelta)
if TYPE_CHECKING_INVALID_USAGE:
series.var() # type: ignore[misc]
Copy link
Contributor

Choose a reason for hiding this comment

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

Never is considered as "valid" by type checkers. However if a function call returns Never, type checkers won't check the code after that call.

You can do something like this:

Suggested change
series.var() # type: ignore[misc]
def _0() -> None: # pyright: ignore[reportUnusedFunction]
assert_type(series.var(), Never)


check(assert_type(series.mean(), pd.Timestamp), pd.Timestamp)
check(assert_type(series.median(), pd.Timestamp), pd.Timestamp)
check(assert_type(series.std(), pd.Timedelta), pd.Timedelta)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add a check here for series.var()

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.

Scalar type to float error with pyright

3 participants