Py::new and .into_py are inconsistent on complex enums
Py::new and .into_py are currently inconsistent.
Note how the constructed value in the example below is not an instance of the specific variant.
This is intended to be fixed before 0.21, see review comment here:
https://github.com/PyO3/pyo3/pull/3582/files#r1451402473
use pyo3::prelude::*;
#[pyclass]
enum MyEnum {
Variant { i: i32 },
}
Python::with_gil(|py| {
let x = Py::new(py, MyEnum::Variant { i: 42 }).unwrap();
let cls = py.get_type::<MyEnum>();
pyo3::py_run!(py, x cls, r#"
assert isinstance(x, cls)
assert not isinstance(x, cls.Variant)
"#)
})
For what it's worth, Py::new currently goes through Into<PyClassInitializer> on the argument. We probably can't easily change that bound, but we might be able to change the Into implementation or the way that the PyClassInitializer then creates the Python object.
Probably either way that this works will involve removing some blanket implementation for T: PyClass and instead implementing it per-class as part of the #[pyclass] macro.