Skip to content

Inheritance example does not compile #381

@Alexander-N

Description

@Alexander-N

🐛 Bug Reports

🌍 Environment

  • Your operating system and version: Ubuntu 18.10
  • Your python version: 3.6.7
  • How did you install python (e.g. apt or pyenv)? apt
  • Did you use a virtualenv?: no
  • Your rust version (rustc --version): rustc 1.34.0-nightly (aadbc459b 2019-02-23)
  • Are you using the latest pyo3 version? Have you tried using latest master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")? yes

💥 Reproducing

The following code from https://pyo3.rs/master/class.html#inheritance

#![allow(unused_variables)]
fn main() {
   use pyo3::prelude::*;
   use pyo3::PyRawObject;
   #[pyclass]
   struct BaseClass {
      val1: usize,
   }

   #[pymethods]
   impl BaseClass {
      #[new]
      fn new(obj: &PyRawObject) {
         obj.init(BaseClass { val1: 10 });
      }

      pub fn method(&self) -> PyResult<()> {
         Ok(())
      }
   }

   #[pyclass(extends=BaseClass)]
   struct SubClass {
      val2: usize,
   }

   #[pymethods]
   impl SubClass {
      #[new]
      fn new(obj: &PyRawObject) {
         obj.init(SubClass { val2: 10 });
         BaseClass::new(obj);
      }

      fn method2(&self) -> PyResult<()> {
         self.get_base().method()
      }
   }
}

gives me

error[E0599]: no method named `get_base` found for type `&main::SubClass` in the current scope
  --> src/main.rs:93:15
   |
93 |          self.get_base().method()
   |               ^^^^^^^^
   |
   = note: the method `get_base` exists but the following trait bounds were not satisfied:
           `&main::SubClass : pyo3::objectprotocol::ObjectProtocol`
           `main::SubClass : pyo3::objectprotocol::ObjectProtocol`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `get_base`, perhaps you need to implement it:
           candidate #1: `pyo3::objectprotocol::ObjectProtocol`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions