-
Notifications
You must be signed in to change notification settings - Fork 763
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 PyDict.update()
and PyDict.update_if_missing()
#2912
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks fine to add this! I think override_
is fine, it matches the C API and isn't strictly speaking part of the API so much as just documentation given Rust doesn't have named arguments.
I have a couple small suggestions to tweak this, and then let's squash & merge 👍
src/types/dict.rs
Outdated
|
||
/// Merge another dictionary into this one. | ||
/// | ||
/// This is equivalent to the Python expression `self.update(other)` if `override` is `true`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's probably worth adding what happens if override_
is false (as I understand it, existing keys are not updated).
src/types/dict.rs
Outdated
/// Merge another dictionary into this one. | ||
/// | ||
/// This is equivalent to the Python expression `self.update(other)` if `override` is `true`. | ||
pub fn merge(&self, other: &PyDict, override_: bool) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like other
is allowed to be &PyMapping
here from the CPython docs, PyDict
has .as_mapping()
conversion so this wouldn't make it significantly harder to use if we allowed the wider type constraint.
Maybe it would be better to use an enum for this, like pub enum Override{
ReplaceSelf,
ReplaceOther,
} This gets rid of that _ too 🤮 |
An enum instead of bool can help with documentation, so I'm fine with doing that. Not sure how it removes |
If you'd like the underscore gone and to use an enum, my suggestion would be to call the parameter enum ReplaceExisting {
No,
Yes,
} |
Surely we just change the variable name to I also think using an enum is significantly uglier than a bool - another import, an unexpected API, and it would make me think there should be more than 2 options. We could use |
Uh yeah...not sure how I thought that
My gut feeling is that this is one of those methods where people are going to mess up. I think I'd have to look up which overrides what half the time 😅 . So maybe splitting this up in multiple methods is a better idea. |
Well in that case we can drop the case of So we could then have:
The question then is what we call We could use |
So I looked in the CPython source and it turns out that So it's correct that there are really only two cases here. Final observation is that these functions support arbitrary mappings, not just fn update(&self, other: &PyMapping) -> PyResult<()>
fn update_if_missing(&self, other: &PyMapping) -> PyResult<()> If you really want fn update(&self, other: &PyDict) -> PyResult<()>
fn update_if_missing(&self, other: &PyDict) -> PyResult<()>
fn update_from_mapping(&self, other: &PyMapping) -> PyResult<()>
fn update_if_missing_from_mapping(&self, other: &PyMapping) -> PyResult<()> But there would be no functional difference, and |
|
PyDict.merge()
and PyDict.update()
PyDict.update()
and PyDict.update_if_missing()
fcec083
to
9155951
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! I'll force-push to squash and then merge.
9155951
to
c09dfcd
Compare
bors r+ |
Awesome, thank you. |
Build succeeded: |
Fix #2910
Note, I'd also be happy to remove the
override_
argument from merge and perhaps rename it toupdate_missing
or similar to give a cleaner API. LMK what you think.Please consider adding the following to your pull request: