Skip to content

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

Description

@scott-wilson

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)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions