Multiple inheritance with slotted parent #3547
-
My knowledge of Python data model is pretty limited, but I was expecting the following would work: #[pyclass(subclass)]
#[derive(Clone, Debug)]
struct A {}
#[pymodule]
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<A>()?;
Ok(())
} from my_module import A
class B:
__slots__ = ('example',)
class C(A, B): ... This fails with: > class C(A, B):
E TypeError: multiple bases have instance lay-out conflict But the rust class Can anyone point me to the right direction of what is happening? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
So the cause here is that PyO3 still adds some extra space to Testing locally, I indeed find this works: #[pyclass(subclass, frozen)] // <--- frozen added here
#[derive(Clone, Debug)]
struct A {}
#[pymodule]
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<A>()?;
Ok(())
} Note that if you make |
Beta Was this translation helpful? Give feedback.
So the cause here is that PyO3 still adds some extra space to
struct A
's Python layout to do our refcell tracking. You can turn this off with#[pyclass(frozen)]
. (We've discussed making frozen the default in the past.)Testing locally, I indeed find this works:
Note that if you make
A
non-empty then again you'll have a conflict. That's just a limitation of Python multiple inheritance, there's not much that can be done about that.