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

types: add PyMapping #1844

Merged
merged 1 commit into from Sep 26, 2021
Merged

types: add PyMapping #1844

merged 1 commit into from Sep 26, 2021

Conversation

davidhewitt
Copy link
Member

Adds a PyMapping type - similar to PySequence, but specially to interact with the mapping protocol.

It doesn't have get_item, set_item, and del_item methods because they're already present because of Deref to PyAny. However it contains a few other helpful pieces such as .contains_key(), .keys(), .values(), and .items().

Seemed like it might be useful for davidhewitt/pythonize#16 (cc @i1i1)

WIP - needs tests.

Copy link
Member

@birkenfeld birkenfeld left a comment

Choose a reason for hiding this comment

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

As I proposed in #1845 for sequences, the API here should mirror PyDict as closely as possible.

K: ToBorrowedObject,
{
let r = key.with_borrowed_ptr(self.py(), |ptr| unsafe {
ffi::PyMapping_HasKey(self.as_ptr(), ptr)
Copy link
Member

Choose a reason for hiding this comment

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

If we use HasKey which suppresses exceptions, the method can return just bool?

Otherwise, use GetItem internally to propagate the exceptions...

@davidhewitt
Copy link
Member Author

👍 will update this to follow what you've done in #1849 but for PyDict / PyMapping

@davidhewitt
Copy link
Member Author

Updated to contain the other relevant PyDict APIs and added tests.

In the end I couldn't come up with a good decision for .contains_key. I felt it was potentially confusing to implement it using PyMapping_HasKey, because the suppressed exception might not be a KeyError but some other cause (e.g. failed comparison when comparing two keys).

So for now I left .contains_key out.

@davidhewitt
Copy link
Member Author

Any objections if I merge this?

Copy link
Member

@birkenfeld birkenfeld left a comment

Choose a reason for hiding this comment

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

Could add an entry in guide/src/conversions/tables.md I guess.

@davidhewitt
Copy link
Member Author

👍 good idea, done!

@davidhewitt davidhewitt merged commit 16ac7d4 into PyO3:main Sep 26, 2021
@davidhewitt davidhewitt deleted the mapping-type branch September 26, 2021 13:56
@Gobot1234
Copy link
Contributor

Is there a reason that contains_key wasn't implemented, it would be much better than obj.getattr("__contains__")?.call1((key,))?.is_true()?

@davidhewitt
Copy link
Member Author

davidhewitt commented Dec 29, 2021

See #1844 (comment)

I just couldn't make an obvious decision on the semantics of contains_key in this initial PR. If you're willing to come up with an appropriate implementation and happy to open a PR for it, I'd be happy to add it to this type.

@birkenfeld
Copy link
Member

Don't we have a method for the in operator on PyAny?

@Gobot1234
Copy link
Contributor

Not that I could find?

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