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

deprecate ToPyObject in favor or IntoPyObject #4595

Merged
merged 2 commits into from
Oct 7, 2024

Conversation

Icxolu
Copy link
Contributor

@Icxolu Icxolu commented Oct 4, 2024

This deprecates ToPyObject in favor or IntoPyObject. All the impls are [allow(deprecated)], tests were adapted to use IntoPyObject. This will probably cause some hit in coverage, but it should hopefully recover once we can remove a lot of this after 0.23.

Since the diff went quite big, let me know if I should try to split this.

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.

Thanks, this looks reasonable to me. I was wondering about punting on this deprecation until 0.24, however I also think given it's just a deprecation there's no harm in kicking it off sooner.

A bunch of thoughts related to documentation, as well as celebrating some nice cleanups! 🎉

guide/src/migration.md Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

We should add IntoPyObject to this file and update the section about ToPyObject to note that it is deprecated. Can leave it with an #[allow(deprecated)] example so that we remember to remove the section along with the trait in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have written docs as part of #4495. I could copy the relevant section into here if we want to merge them sooner.

src/conversion.rs Outdated Show resolved Hide resolved
//!
//! fn main() {
//! fn main() -> PyResult<()> {
Copy link
Member

Choose a reason for hiding this comment

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

Nice touch to make these examples fallible and remove unwraps 👍

Comment on lines +965 to +968
// Also check that trying to convert an out of bound value errors.
Python::with_gil(|py| {
assert!(panic::catch_unwind(|| Duration::min_value().to_object(py)).is_err());
assert!(panic::catch_unwind(|| Duration::max_value().to_object(py)).is_err());
assert!(Duration::min_value().into_pyobject(py).is_err());
assert!(Duration::max_value().into_pyobject(py).is_err());
Copy link
Member

Choose a reason for hiding this comment

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

Much nicer 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's really nice to see these finally go 🥳

src/conversions/either.rs Outdated Show resolved Hide resolved
src/conversions/either.rs Outdated Show resolved Hide resolved
@@ -194,8 +198,7 @@ mod tests {
let mut map = hashbrown::HashMap::<i32, i32>::new();
map.insert(1, 1);

let m = map.to_object(py);
let py_map = m.downcast_bound::<PyDict>(py).unwrap();
let py_map = (&map).into_pyobject(py).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

❤️ it's nice to see the downcasts removed, even if there's suddenly a new fallible case!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! And getting a Bound directly instead of a Py is also super nice

@Icxolu Icxolu force-pushed the deprecate/to-pyobject branch from af09744 to b04ca9e Compare October 5, 2024 09:18
@Icxolu Icxolu force-pushed the deprecate/to-pyobject branch from b04ca9e to a9fb13c Compare October 5, 2024 09:21
@Icxolu Icxolu force-pushed the deprecate/to-pyobject branch from a9fb13c to d81d00f Compare October 5, 2024 14:24
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.

Looks good to me, thanks!

Yes, sorry I'd forgotten the docs were part of the derive macro PR. I will loop back to that ASAP, also need to figure out the next steps to get freethreading ready for 0.23 and the 0.22.4 bug fixes landed 😱

@davidhewitt davidhewitt added this pull request to the merge queue Oct 7, 2024
Merged via the queue into PyO3:main with commit 4f779c8 Oct 7, 2024
42 of 43 checks passed
@Icxolu Icxolu deleted the deprecate/to-pyobject branch October 8, 2024 16:11
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