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

Improvements to property macros #760

Merged
merged 6 commits into from
Feb 10, 2020
Merged

Conversation

davidhewitt
Copy link
Member

There are two main fixes that I've added to the property macro code:

  • #[pyo3(get)] now works with PyObject fields
  • #[setter] functions can now take a Python argument

After implementing the first I'd changed enough of the code such that cleaning it up caused the second to be fixed trivially.

I also bumped the minimum Rust version to 1.42.0-nightly 2020-01-21, to make use of the recently stabilized #![feature(slice_patterns)].

@davidhewitt
Copy link
Member Author

The CI failures are from new clippy lints from the newer rust version. I've added a PR #761 to fix those separately.

T: IntoPy<PyObject> + Clone,
{
fn get_property_value(&self, py: Python) -> PyObject {
(*self).clone().into_py(py)
Copy link
Member

Choose a reason for hiding this comment

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

Why Deref here... ?

Copy link
Member Author

Choose a reason for hiding this comment

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

&self is &&T, so self.clone() makes &T. We need to deref like (*self).clone() to get a T.

pyo3-derive-backend/src/pymethod.rs Outdated Show resolved Hide resolved
pyo3-derive-backend/src/pymethod.rs Outdated Show resolved Hide resolved
src/derive_utils.rs Outdated Show resolved Hide resolved
@kngwyu
Copy link
Member

kngwyu commented Feb 9, 2020

Thank you!
I left some comments but generally LGTM 👍
But it looks you update minimum nightly also in #761


impl<T> GetPropertyValue for &T
where
T: IntoPy<PyObject> + Clone,
Copy link
Member Author

@davidhewitt davidhewitt Feb 9, 2020

Choose a reason for hiding this comment

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

I was wondering whether the Clone bound should really be Copy, so that we don't let people accidentally do really expensive properties with #[pyo3(get)] (such as Vec<T>).

Users can always write their own #[getter] functions for types which aren't Copy.

@davidhewitt
Copy link
Member Author

Thanks, I've made some changes to address your comments. I suggest we wait for #761 to merge first. After that I'll fix up the merge conflicts in this PR and then we can potentially merge this.

@kngwyu
Copy link
Member

kngwyu commented Feb 10, 2020

Thank you!

@kngwyu kngwyu merged commit 3c02de2 into PyO3:master Feb 10, 2020
@davidhewitt davidhewitt deleted the property-fixes branch February 10, 2020 14:46
@davidhewitt
Copy link
Member Author

Thanks for fixing the merge conflicts!

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.

2 participants