Skip to content

fix(python): Raise TypeError on GroupBy next() call#27324

Open
yosofbadr wants to merge 3 commits intopola-rs:mainfrom
yosofbadr:fix/groupby-next-error
Open

fix(python): Raise TypeError on GroupBy next() call#27324
yosofbadr wants to merge 3 commits intopola-rs:mainfrom
yosofbadr:fix/groupby-next-error

Conversation

@yosofbadr
Copy link
Copy Markdown

@yosofbadr yosofbadr commented Apr 15, 2026

Summary

  • Fixes next() on a GroupBy object raising AttributeError: 'GroupBy' object has no attribute '_current_index' instead of a proper TypeError.
  • Extracts iteration state into a new _GroupByIter class. GroupBy.__iter__() now returns a _GroupByIter instance instead of self, so the GroupBy class itself no longer implements __next__. This means next(df.group_by(...)) raises TypeError: 'GroupBy' object is not an iterator, matching standard Python iterator protocol behavior.
  • Applies the same fix to RollingGroupBy and DynamicGroupBy which had the identical issue.

Closes #12868

Test plan

  • Added test_group_by_next_raises_type_error_12868 to verify next() on a GroupBy raises TypeError
  • Existing test_group_by_iteration tests should continue passing (iteration via for loop and iter() still works)
  • test_group_by_all_12869 which uses next(iter(...)) pattern should pass

Extract iteration state into a separate _GroupByIter class so that
GroupBy, RollingGroupBy, and DynamicGroupBy no longer implement
__next__ directly. This means calling next() on a GroupBy object
without first calling iter() now raises a clear TypeError instead
of an AttributeError about a missing _current_index attribute.

Closes pola-rs#12868
@github-actions github-actions bot added fix Bug fix python Related to Python Polars first-contribution First contribution by user title needs formatting labels Apr 15, 2026
@yosofbadr yosofbadr changed the title fix(python): raise TypeError on GroupBy next() call fix(python): Raise TypeError on GroupBy next() call Apr 15, 2026
- Sort __slots__ alphabetically in _GroupByIter (RUF023)
- Add type: ignore[call-overload] for intentional next(GroupBy) test
@yosofbadr yosofbadr marked this pull request as ready for review April 16, 2026 04:39
The coverage-rust job timed out after 37 minutes; our change is
Python-only and unrelated to Rust coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-contribution First contribution by user fix Bug fix python Related to Python Polars

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next() on GroupBy raises AttributeError object has no attribute _current_index

1 participant