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 PyAnyMethods.try_iter and deprecate .iter #4553

Merged
merged 3 commits into from
Oct 5, 2024

Conversation

LilyFoote
Copy link
Contributor

Fixes #4550.

@LilyFoote LilyFoote added bugfix confusing-api rust Pull requests that update Rust code labels Sep 13, 2024
@LilyFoote LilyFoote self-assigned this Sep 13, 2024
@davidhewitt
Copy link
Member

Thanks, sounds like try_iter is the preferred choice. While we're here, perhaps let's add some detail to the documentation to make it more clear that you need to handle the result before trying to iterate? (An example might be nice.)

@LilyFoote
Copy link
Contributor Author

I've added an example, but I'm open to a better one.

@mejrs
Copy link
Member

mejrs commented Sep 14, 2024

I've added an example, but I'm open to a better one.

I nominate:

fn does_it_have_the_answer(obj: &Bound<'_, PyAny>) -> bool {
    if obj.extract() == Some(42_u8) {
        true
    } else if obj.extract() == Some("42") {
        true
    } else if let Ok(iter) = obj.try_iter() {
        for item in iter {
            if let Ok(item) = item {
                if does_it_have_the_answer(item) {
                    return true;
                }
            }
        }
    }
    false
}

(maybe add some comments to explain what we're doing)

@Icxolu
Copy link
Contributor

Icxolu commented Sep 15, 2024

For my taste that does have a bit too much complexity that is not really relevant for the fallibility of the iterator. I would prefer something a bit simpler, but I don't have a good suggestion in mind right now... At least I think the try_iter call should be at the start of the example and introducing recursion does not feel particularly necessary.

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 as we're a bit stuck of ideas for the perfect example, let's just proceed to merge. I'd prefer see this get into 0.23 with a slightly subpar example and refine another time. Thanks all!

@davidhewitt davidhewitt added this pull request to the merge queue Oct 4, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Oct 4, 2024
@davidhewitt davidhewitt added this pull request to the merge queue Oct 4, 2024
Merged via the queue into PyO3:main with commit 75bd3f2 Oct 5, 2024
42 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix confusing-api rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PyAnyMethods::iter() is a footgun, should be called try_iter()
4 participants