Skip to content

Add IntoIterator for &Bound types#3923

Merged
davidhewitt merged 5 commits intoPyO3:mainfrom
LilyFirefly:list-iter
Mar 4, 2024
Merged

Add IntoIterator for &Bound types#3923
davidhewitt merged 5 commits intoPyO3:mainfrom
LilyFirefly:list-iter

Conversation

@LilyFirefly
Copy link
Copy Markdown
Contributor

@LilyFirefly LilyFirefly commented Mar 2, 2024

This allows using common types like &Bound<'py, PyList> in a for loop without explicitly calling iter().

@LilyFirefly LilyFirefly self-assigned this Mar 2, 2024
@LilyFirefly LilyFirefly added the CI-skip-changelog Skip checking changelog entry label Mar 2, 2024
Copy link
Copy Markdown
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

This looks useful to me, maybe it would make sense to also do this for the other iterables (PyTuple, Py(Frozen)Set and PyDict come to mind)?

@LilyFirefly LilyFirefly changed the title Add IntoIterator for &Bound<'py, PyList> Add IntoIterator for &Bound types Mar 2, 2024
@davidhewitt
Copy link
Copy Markdown
Member

Thanks, I think these do make sense, I since remembered that I hit this a fair bit in pydantic-core when testing out the new Bound API there.

Some history on why these didn't yet exist is in #3695 (comment) and #3694 (comment) - basically I think @adamreichold and I had held back on adding these previously because I'd forgotten the use case at the time :)

I think the only question I have left remaining is what to do about &Bound<'_, PyIterator>? It might be nice to have IntoIterator for that one too, but I'm less sure.

Copy link
Copy Markdown
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.

I think let's merge this as-is. I think the only possible design question is whether we would want to have extra iterator types that contain &Bound instead of Bound.

The only advantage of the extra types would be to be able to avoid one reference count operation, and iteration necessarily is N reference counting ops anyway. Due to this I think it's not worth bothering with the extra complexity of additional types.

@davidhewitt davidhewitt added this pull request to the merge queue Mar 4, 2024
Merged via the queue into PyO3:main with commit 811a3e5 Mar 4, 2024
@LilyFirefly LilyFirefly deleted the list-iter branch March 4, 2024 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-skip-changelog Skip checking changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants