Skip to content

fix(python): Raise TypeError instead of AttributeError when calling next() on uninitialized GroupBy#27349

Open
avasis-ai wants to merge 1 commit intopola-rs:mainfrom
avasis-ai:fix-groupby-next-attributeerror
Open

fix(python): Raise TypeError instead of AttributeError when calling next() on uninitialized GroupBy#27349
avasis-ai wants to merge 1 commit intopola-rs:mainfrom
avasis-ai:fix-groupby-next-attributeerror

Conversation

@avasis-ai
Copy link
Copy Markdown

Summary

Calling next() on a GroupBy, RollingGroupBy, or DynamicGroupBy object without first iterating over it raises an unhelpful AttributeError: 'GroupBy' object has no attribute '_current_index'. This PR changes the error to a clear TypeError, consistent with standard Python behavior for non-iterators.

import polars as pl
next(pl.DataFrame().group_by(1))
# Before: AttributeError: 'GroupBy' object has no attribute '_current_index'
# After:  TypeError: 'GroupBy' object is not an iterator

Changes

Added a hasattr check at the start of __next__ in GroupBy, RollingGroupBy, and DynamicGroupBy to raise a descriptive TypeError when iteration state hasn't been initialized.

Closes #12868

@github-actions github-actions Bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars first-contribution First contribution by user title needs formatting labels Apr 19, 2026
@alexander-beedie alexander-beedie changed the title fix: raise TypeError instead of AttributeError when calling next() on uninitialized GroupBy fix(python): Raise TypeError instead of AttributeError when calling next() on uninitialized GroupBy Apr 25, 2026
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 rust Related to Rust 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