Skip to content

#[derive(IntoPyObject)] error a bit unhelpful when lifetime not called 'py #4704

Open
@davidhewitt

Description

In pydantic-core some (legacy) lifetimes were 'a instead of 'py, and using the new #[derive(IntoPyObject)] led to some interesting errors:

use pyo3::{Bound, IntoPyObject, PyAny};

#[derive(IntoPyObject)]
pub struct Wrap<'a>(Bound<'a, PyAny>);

gets lots of errors like

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'py` due to conflicting requirements
 --> src/lib.rs:3:10
  |
3 | #[derive(IntoPyObject)]
  |          ^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
 --> src/lib.rs:4:17
  |
4 | pub struct Wrap<'a>(Bound<'a, PyAny>);
  |                 ^^
note: ...but the lifetime must also be valid for the lifetime `'py` as defined here...
 --> src/lib.rs:3:10
  |
3 | #[derive(IntoPyObject)]
  |          ^^^^^^^^^^^^
note: ...so that the types are compatible
 --> src/lib.rs:3:10
  |
3 | #[derive(IntoPyObject)]
  |          ^^^^^^^^^^^^
  = note: expected `pyo3::IntoPyObject<'py>`
             found `pyo3::IntoPyObject<'_>`
  = note: this error originates in the derive macro `IntoPyObject` (in Nightly builds, run with -Z macro-backtrace for more info)

changing the struct lifetime from 'a to 'py trivially resolves, though I wonder if there's any tricks we can do to improve the UX 🤔

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions