Skip to content

Misleading error: Missing trait IntoPyCallbackOutput #1500

@mejrs

Description

@mejrs

First of all, great crate :)

Just now, I was trying to get this pyclass/ Rust struct to compile. It's basically a struct around a hashmap that I want to expose a Python .get method for.

#[pyclass]
#[derive(Debug, Clone)]
pub struct ParamTable {
    #[doc(hidden)]
    pub params: HashMap<u32, Param>,
}

#[pymethods]
impl ParamTable{
	pub fn get(&self, id: u32) -> PyResult<Option<&Param>>{
		Ok(self.params.get(&id))
	}
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Param {
    Integer(i32),
    String(String),
}

This results in the following compilation error:

error[E0277]: the trait bound `Result<std::option::Option<&structures::Param>, PyErr>: IntoPyCallbackOutput<_>` is not satisfied
   --> src\cache\structures.rs:37:1
    |
37  | #[pymethods]
    | ^^^^^^^^^^^^ the trait `IntoPyCallbackOutput<_>` is not implemented for `Result<std::option::Option<&structures::Param>, PyErr>`
    |
   ::: C:\Users\bruno\.cargo\registry\src\github.com-1ecc6299db9ec823\pyo3-0.13.2\src\callback.rs:170:8
    |
170 |     T: IntoPyCallbackOutput<U>,
    |        ----------------------- required by this bound in `pyo3::callback::convert`
    |
    = help: the following implementations were found:
              <Result<T, E> as IntoPyCallbackOutput<U>>
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

Sadly, IntoPyCallbackOutput has no documentation and can't be found in the user guide.

I eventually figured out that I needed to implement IntoPy:

impl IntoPy<PyObject> for &Param {
    fn into_py(self, py: Python) -> PyObject {
        match self{
			Param::Integer(val) => val.into_py(py),
			Param::String(val) => val.into_py(py),
		}
    }
}

Is this something that can be fixed? It did take me a while to figure out.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions