Skip to content

Allow non-ExactSizeIterator in PyList::new#5830

Merged
davidhewitt merged 3 commits intoPyO3:mainfrom
bschoenmaeckers:list-new-not-exact-size
Feb 18, 2026
Merged

Allow non-ExactSizeIterator in PyList::new#5830
davidhewitt merged 3 commits intoPyO3:mainfrom
bschoenmaeckers:list-new-not-exact-size

Conversation

@bschoenmaeckers
Copy link
Member

As discussed in #5822

xref #4782

}
}

impl ExactSizeIterator for FaultyIter {
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we can just drop this impl?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@Tpt Tpt left a comment

Choose a reason for hiding this comment

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

Thank you!

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, this seems like a positive improvement to me, I remember wanting it in the past.

I just checked what Vec does when extending and it looks like it'll try to reserve at least the lower bound, so for cases where we've exceeded the iterator lower bound it'll just be equivalent to pushing element-by-element and resizing when needed, like here.

Ok::<_, PyErr>(count + 1)
})?;

assert_eq!(len, count, "Attempted to create PyList but `elements` was smaller than reported by its `size_hint` implementation.");
Copy link
Member

@davidhewitt davidhewitt Feb 18, 2026

Choose a reason for hiding this comment

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

I'm still trying to decide about this assertion. On one hand, if users have bad iterator implementations, this panic will help them clean up.

On the other hand, I still think it might be user-friendly to just truncate the list to count length.

Maybe for now we just document that we will panic if the iterator passes fewer elements than the size hint (and that we may change that in the future)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Message about the panic should already be in the docs of this function

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks!

@davidhewitt davidhewitt added this pull request to the merge queue Feb 18, 2026
Merged via the queue into PyO3:main with commit f519845 Feb 18, 2026
43 of 45 checks passed
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.

3 participants

Comments