PyO3 / PyO3/pyo3

overflow evaluating requirement for `&pyo3::Bound<'_, _>: pyo3::IntoPyObject<'_>`

Open
#4,702 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Found while attempting to upgrade pydantic-core to test out PyO3 main.

Consider the following code from 0.22.

use pyo3::prelude::*;

pub trait Foo<'py>: ToPyObject {}

fn takes_foo<'py>(foo: impl Foo<'py>) {}

... this compiles fine, and is a useful pattern. Unfortunately, the upgrade path for this seems difficult in our current 0.23 draft.

Given the ToPyObject super trait (i.e. by-ref conversion), the simple thing I tried is:

use pyo3::prelude::*;

pub trait Foo<'py>
where
    for<'a> &'a Self: IntoPyObject<'py>,
{
}

fn takes_foo<'py>(foo: impl Foo<'py>) {}

Unfortunately, this immediately blows up the compiler with a recursion error:

error[E0275]: overflow evaluating the requirement `&pyo3::Bound<'_, _>: pyo3::IntoPyObject<'_>`
  |
  = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`pyo3_scratch`)
  = note: required for `&&pyo3::Bound<'_, _>` to implement `pyo3::IntoPyObject<'_>`
  = note: 126 redundant requirements hidden
  = note: required for `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...` to implement `for<'a> pyo3::IntoPyObject<'py>`
  = note: the full name for the type has been written to '/Users/david/Dev/pyo3-scratch/target/debug/deps/pyo3_scratch.long-type-2493199481127865716.txt'
  = note: consider using `--verbose` to print the full type name to the console

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standalone Rust reproducer in the issue and compare the 0.22 ToPyObject pattern with the 0.23 IntoPyObject bound. Trace the recursive trait requirements reported by the compiler; done means an equivalent trait pattern compiles without overflow and provides a viable upgrade path. No source file or test is named.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.