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

Bump to 0.23 #21

Merged
merged 11 commits into from
Nov 21, 2024
Merged

Bump to 0.23 #21

merged 11 commits into from
Nov 21, 2024

Conversation

kylebarron
Copy link
Contributor

@kylebarron kylebarron commented Nov 18, 2024

Change list

  • Bump pyo3-async-runtimes version in Cargo.toml and documentation to 0.23
  • Bump pyo3 dependency version to 0.23
  • Switched to new IntoPyObject trait, with a couple compilation errors still that I'm not sure how to fix.
  • Fixed lints to remove _bound suffix

@kylebarron
Copy link
Contributor Author

I tried two slightly different approaches that both hit small snags. It seems like we need some bound on the Target associated type of IntoPyObject, but we probably also want that to be quite flexible? So the latest two commits added a new generic with a bound of AsRef<PyAny>. And that seemed to help, just getting a single compilation error now that I don't know how to fix.

src/generic.rs Outdated Show resolved Hide resolved
@kylebarron
Copy link
Contributor Author

I noticed PyO3/pyo3#4708, which helped me fix compilation here without needing to add another generic.

Copy link

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

Thanks for helping with the migration!

I have some comments on the IntoPyObject migration. The majority of the bounds introduced here are stricter then necessary. The higher ranked bound (for<'py> ...) means the bound must hold for any arbitrary lifetime. But most of the time you already have a specific 'py lifetime as an input via the Python<'py> token, a Bound<'py, T> or a Borrowed<'_, 'py, T>. In these case it is only necessary to restrict the bound to that specific lifetime instead of all possible lifetimes.

The higher ranked bound is needed when you don't have any input that brings a 'py lifetime. In these cases the Python<'py> token is usually created via Python::with_gil which can have any arbitrary lifetime.

I hope this clears it a bit up and maybe solves the compile errors you were running into.
(PyO3/pyo3#4708 should help with boilerplate, but everything should be possible to implement in the mean time without it)

src/async_std.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/generic.rs Show resolved Hide resolved
@kylebarron
Copy link
Contributor Author

@Icxolu I tried to move to the 'py lifetime defined on the function, but that gave an error of

error[E0521]: borrowed data escapes outside of closure
    --> src/generic.rs:1033:49
     |
989  | pub fn local_future_into_py_with_locals<'py, R, F, T>(
     |                                         --- lifetime `'py` defined here
...
1022 |             Python::with_gil(move |py| {
     |                                    --
     |                                    |
     |                                    `py` is a reference that is only valid in the closure body
     |                                    has type `pyo3::Python<'1>`
...
1033 |                     result.and_then(|val| match val.into_pyobject(py) {
     |                                                 ^^^^^^^^^^^^^^^^^^^^^
     |                                                 |
     |                                                 `py` escapes the closure body here
     |                                                 argument requires that `'1` must outlive `'py`

like you said here.

But then when I switch just those two functions in generic.rs that internally call Python::with_gil to use the higher-ranked lifetime, I get an error here saying

error[E0277]: `T` cannot be converted to a Python object
    --> src/generic.rs:1199:46
     |
1199 |     local_future_into_py_with_locals::<R, F, T>(py, get_current_locals::<R>(py)?, fut)
     |                                              ^ the trait `for<'py> pyo3::IntoPyObject<'py>` is not implemented for `T`
     |
     = note: `IntoPyObject` is automatically implemented by the `#[pyclass]` macro
     = note: if you do not wish to have a corresponding Python type, implement it manually
     = note: if you do not own `T` you can perform a manual conversion to one of the types in `pyo3::types::*`
note: required by a bound in `generic::local_future_into_py_with_locals`
    --> src/generic.rs:997:8
     |
989  | pub fn local_future_into_py_with_locals<R, F, T>(
     |        -------------------------------- required by a bound in this function
...
997  |     T: for<'py> IntoPyObject<'py>,
     |        ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `local_future_into_py_with_locals`

So it seems that the higher-ranked for<'py> "infects" the rest of the functions. That's why I originally had the higher-ranked lifetime to get it to compile.

@Icxolu
Copy link

Icxolu commented Nov 18, 2024

Ooops, I guess I spoke to soon then, sorry about that 🙈. On closer look pyo3-async-runtimes is a bit unique in the regard that it passes around Python tokes, while still needing to use Python::with_gil because it spawns, potentially crossing thread boundaries. In that case the higher ranked bound is indeed needed. (Looking at the signatures alone it just looked wrong 🙃)

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, overall this seems to be going in the right direction; looks like the pytests directory needs a bit of love too.

src/lib.rs Outdated Show resolved Hide resolved
@kylebarron
Copy link
Contributor Author

Tests seem to be passing locally now

@kylebarron
Copy link
Contributor Author

I'm not sure why the coverage CI is erroring. It seems to be erroring before even getting a result?

@davidhewitt
Copy link
Member

I think -Zprofile setting has been removed from nightly now that we have -C instrument-coverage, let me see if I can fix...

@davidhewitt davidhewitt mentioned this pull request Nov 20, 2024
@davidhewitt
Copy link
Member

Looks like this is all green, thanks for the help!

@davidhewitt davidhewitt merged commit 4ab275c into PyO3:main Nov 21, 2024
30 checks passed
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.

3 participants