Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Subclass imported python classes #1410

Closed
ghost opened this issue Jan 29, 2021 · 1 comment
Closed

Feature request: Subclass imported python classes #1410

ghost opened this issue Jan 29, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Jan 29, 2021

🌟 Feature request

The doc describes how to subclass from "classes" created in rust, however either the feature isn't there yet or the docs are missing on how to inherit from an imported subclass. It would be great if either the doc were extended or subclassing imported classes made possible.

🌍 Environment

  • Your operating system and version: openSUSE Leap 15.2
  • Your python version: 3.6.12
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?:
    • sudo zypper in -y python3-devel
  • Your Rust version (rustc --version): rustc 1.45.0 (5c1f21c3b 2020-07-13)
  • Your PyO3 version: 0.13.1
  • Have you tried using latest PyO3 master (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes

💥 Reproducing

My rudimentary rust knowledge won't let me get around the errors generated in this example

Cargo.toml

[package]
name = "rust-in-django"
version = "0.1.0"
authors = ["LoveIsGrief <>"]
edition = "2018"


[lib]
name = "rust_in_django"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.13.1"
features = [
    "auto-initialize",
    "extension-module",
]

src/lib.rs

use pyo3::prelude::*;
use pyo3::types::PyType;

#[pyclass]
struct Something {}

/// A Python module implemented in Rust.
#[pymodule]
fn rust_in_django(py: Python, m: &PyModule) -> PyResult<()> {
    let result = py.import("django.db.models").unwrap();
    let model = result.get("Model").unwrap();


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

        #[classmethod]
        fn say(cls: &PyType) {
            println!("printed something");
        }
    }
    m.add_class::<Something>()?;

    Ok(())
}
error[E0433]: failed to resolve: use of undeclared type or module `model`
  --> src/lib.rs:18:38
   |
18 |             PyClassInitializer::from(model::new())
   |                                      ^^^^^ use of undeclared type or module `model`

error[E0433]: failed to resolve: use of undeclared type or module `model`
  --> src/lib.rs:18:38
   |
18 |             PyClassInitializer::from(model::new())
   |                                      ^^^^^ use of undeclared type or module `model`

error[E0271]: type mismatch resolving `<pyo3::types::any::PyAny as pyo3::type_object::PyTypeInfo>::Initializer == pyo3::pyclass_init::PyClassInitializer<pyo3::types::any::PyAny>`
  --> src/lib.rs:19:18
   |
19 |                 .add_subclass(Something{})
   |                  ^^^^^^^^^^^^ expected struct `pyo3::pyclass_init::PyNativeTypeInitializer`, found struct `pyo3::pyclass_init::PyClassInitializer`
   |
   = note: expected struct `pyo3::pyclass_init::PyNativeTypeInitializer<pyo3::types::any::PyAny>`
              found struct `pyo3::pyclass_init::PyClassInitializer<pyo3::types::any::PyAny>`
@davidhewitt
Copy link
Member

Thanks. This is a duplicate of #991 - indeed the feature is not implemented yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant