-
Notifications
You must be signed in to change notification settings - Fork 796
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
Subclassing pyclass from python - new returns wrong type #947
Comments
A minimum example which reproduces this: On the Rust side: use pyo3::prelude::*;
#[pyclass(subclass)]
struct PyClass {}
#[pymethods]
impl PyClass {
#[new]
fn new() -> Self {
Self{}
}
fn get_source(&self) -> PyResult<&'static str> {
Ok("Rust")
}
}
#[pymodule]
fn example(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PyClass>()?;
Ok(())
} On the Python side: from example import PyClass
class Class(PyClass):
def __new__(cls):
return super().__new__(cls) # This should return an instance of Class
def get_source(self):
return "Python"
a = PyClass()
# Should print "Rust":
print(a.get_source())
# Rust
b = Class()
# Should print "Python":
print(b.get_source())
# Rust |
I'm not sure we can fix this 🤔 In [9]: class SubClass(dict):
...: def __new__(cls):
...: return super().__new__(cls)
...:
In [10]: SubClass()
Out[10]: {} |
Interesting. I have a feeling that C++'s |
@kngwyu I'm not sure the dict repr proves that the object isn't of type SubClass. |
@birkenfeld In [6]: class SubClass(dict):
...: def __new__(cls):
...: return super().__new__(cls)
...:
...:
In [7]: c = SubClass()
In [8]: c.__class__
Out[8]: __main__.SubClass |
I found a problem with our |
Originally posted by @thesketh in #945 (comment)
This needs investigating, sounds like a possible bug.
The text was updated successfully, but these errors were encountered: