Skip to content

Python<'_> is not a valid argument to async #[pyfunction]s #5682

@davidhewitt

Description

@davidhewitt

The following code fails to compile:

#[pyfunction]
async fn none(py: Python<'_>) -> Bound<'_, PyAny> {
    py.None().into_bound(py)
}

The error message is:

error[E0521]: borrowed data escapes outside of function
 --> src/lib.rs:7:5
  |
7 |     #[pyfunction]
  |     ^^^^^^^^^^^^^
  |     |
  |     `py` is a reference that is only valid in the function body
  |     `py` escapes the function body here
  |     lifetime `'py` defined here
  |     argument requires that `'py` must outlive `'static`
  |
  = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error: future cannot be sent between threads safely
  --> src/lib.rs:7:5
   |
 7 |     #[pyfunction]
   |     ^^^^^^^^^^^^^ future created by async block is not `Send`
   |
   = help: within `{async block@src/lib.rs:7:5: 7:18}`, the trait `Send` is not implemented for `*mut pyo3::Python<'static>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions