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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

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