Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IntoIterator for &Bound types #3923

Merged
merged 5 commits into from
Mar 4, 2024
Merged

Conversation

LilyFoote
Copy link
Contributor

@LilyFoote LilyFoote commented Mar 2, 2024

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

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

@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)?

@LilyFoote LilyFoote changed the title Add IntoIterator for &Bound<'py, PyList> Add IntoIterator for &Bound types Mar 2, 2024
@davidhewitt
Copy link
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
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
42 checks passed
@LilyFoote LilyFoote 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