PyO3 / PyO3/pyo3

Error Message for methods that return Self for classes that are subclasses of other classes.

Open
#6,197 0 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

I have a class that inherits from another class (See below code snippet) with a __next__ method that returns Self. It threw the error:

error[E0277]: the trait bound `Child: pyo3::impl_::callback::IntoPyCallbackOutput<'_, _>` is not satisfied
   --> src/lib.rs:11:1
    |
 11 | #[pymethods]
    | ^^^^^^^^^^^^ unsatisfied trait bound
    |
help: the trait `pyo3::impl_::callback::IntoPyCallbackOutput<'_, _>` is not implemented for `Child`
   --> src/lib.rs:9:1
    |
  9 | struct Child(u64);
    | ^^^^^^^^^^^^
    = help: the following other types implement trait `pyo3::impl_::callback::IntoPyCallbackOutput<'py, Target>`:
              ...
note: required by a bound in `pyo3::impl_::pymethods::IterBaseTag::convert`
   --> .../src/impl_/pymethods.rs:473:16
    |
471 |     pub fn convert<'py, Value, Target>(self, py: Python<'py>, value: Value) -> PyResult<Target>
    |            ------- required by a bound in this associated function
472 |     where
473 |         Value: IntoPyCallbackOutput<'py, Target>,
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `IterBaseTag::convert`
    = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `example_py` (lib) due to 1 previous error

The fix was to have the method return Py::new(py, PyClassInitializer::from(Base).add_subclass(self.0)), but it would be nice to have the compiler error message report this.

Example code that threw the compile error:

#[pyclass(subclass)]
#[derive(Debug, Clone, Copy)]
struct Base;

#[pyclass(extends=Base, subclass)]
#[derive(Debug, Clone, Copy)]
struct Child(u64);

#[pymethods]
impl Child {
    #[new]
    fn new() -> PyClassInitializer<Self> {
        PyClassInitializer::from(Base).add_subclass(Self(0))
    }

    fn __next__(&mut self) -> Self {
        self.0 += 1;

        Self(self.0)
    }
}

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

Reproduce the example from src/lib.rs, then inspect the #[pymethods] expansion and src/impl_/pymethods.rs around IterBaseTag::convert and its IntoPyCallbackOutput bound. Trace how the Self return is diagnosed and add coverage for subclassed PyClasses; done means the compiler output explains the required wrapped return form instead of only reporting the trait bound.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.