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

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

Open
davidhewitt opened this issue Nov 14, 2024 · 3 comments

Comments

@davidhewitt
Copy link
Member

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 🤔

@alex
Copy link
Contributor

alex commented Nov 14, 2024

I haven't looked at the impl, but FWIW what I do in rust-asn1 is to look if the struct already has a lifetime, and if so use that lifetime.

@Icxolu
Copy link
Contributor

Icxolu commented Nov 14, 2024

The reason for the special case is that we do support lifetimes other than 'py, eg this works:

#[derive(IntoPyObject)]
struct Foo<'a>(&'a str);

But I can have a look if there is a way to improve the error message.

@davidhewitt
Copy link
Member Author

Yes, I thought it was something like that special case where the lifetime is unrelated 👍

One thought I had is that maybe with GATs the lifetime would be unnecessary on the trait and could go on the Output associated type. Covariance might allow things to just work. That's a future possibility though rather than something we can use now.

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

No branches or pull requests

3 participants