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

move PyDict::get_item_with_error to PyDict::get_item #3330

Merged
merged 4 commits into from Sep 10, 2023

Conversation

davidhewitt
Copy link
Member

As per capi-workgroup/problems#51 the recommendation of the Python core devs is to avoid PyDict_GetItem because it can hide arbitrary exceptions.

Accordingly I've moved the PyDict::get_item_with_error to just become PyDict::get_item, and deprecated the get_item_with_error name.

@davidhewitt
Copy link
Member Author

This is obviously quite an irritating change for users because it changes the return type from Option<&PyAny> to PyResult<Option<&PyAny>>. This means adding an extra .unwrap() in a lot of our test code. For users it's probably less bad because they can hopefully add ? to propagate the exception.

I still defend the change as worthwhile given it nudges them onto using a "correct" API instead of unintentionally ignoring inner errors.

Copy link
Member

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

+1 for this change.

It'd be nice to flesh out the docs. e.g. to answer questions like

  • is it OK to unwrap or unwrap_or(None)?
  • should I just bubble up the error?
  • when can this return an error?

src/types/dict.rs Outdated Show resolved Hide resolved
@davidhewitt
Copy link
Member Author

Sorry for the long delay here. I've added some detail to the documentation to cover the points raised above, they were all good suggestions 👍

@davidhewitt
Copy link
Member Author

As this has had an approval and I think any further changes which might be requested from further reviews would be docs rather than API, I'm going to merge this now so that I can start building an 0.20 release branch.

@davidhewitt davidhewitt added this pull request to the merge queue Sep 10, 2023
Merged via the queue into PyO3:main with commit 0ab00c7 Sep 10, 2023
32 checks passed
@davidhewitt davidhewitt deleted the get-item-with-error branch September 10, 2023 22:06
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.

None yet

3 participants