PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.36k stars 761 forks source link

Error: TypeError: 'Kind' object cannot be converted to 'Kind' #3513

Closed huskyrobotdog closed 1 year ago

huskyrobotdog commented 1 year ago

Complex type conversion seems to go wrong.

Error: TypeError: 'Kind' object cannot be converted to 'Kind'

Rust :

#[pyclass]
#[derive(Debug, Clone, PartialEq, EnumString, Display, Serialize, Deserialize)]
#[strum(serialize_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum Kind {
    Spot,
    Contract,
}

Python :

call() -> List[Tuple[Kind, str, Decimal, Decimal, Decimal]]:
    result = []
    result.append((Kind.Spot,'', Decimal(0), Decimal(0), Decimal(0)))
    result

Call :

let result = result.as_ref(py).extract()?;
huskyrobotdog commented 1 year ago

The type I want to get

Vec<(Kind, String, Decimal, Decimal, Decimal)>
adamreichold commented 1 year ago

Error: TypeError: 'Kind' object cannot be converted to 'Kind'

Could you post the full compilerinterpreter output. Usually such errors indicate that you use Kind instances from two different PyO3-based extensions. (Due to static linking, they do not share types even if their Rust implementations do, i.e. Kind from one extension has a different type than the Kind from the other.)

huskyrobotdog commented 1 year ago

Error: TypeError: 'Kind' object cannot be converted to 'Kind'

Could you post the full ~compiler~interpreter output. Usually such errors indicate that you use Kind instance from two different PyO3-based extensions. (Due to static linking, they do not share types even if their Rust implementations do, i.e. Kind from one extension has a different type than the Kind from the other.)

Export enum as python class via pyo3. Then use rust to call the python function, and the enum(Kind.Spot) returned by python cannot be converted to Rust enum(Kind::Spot) through extract. Tip: 'Kind' object cannot be converted to 'Kind'

davidhewitt commented 1 year ago

Without a full minimal reproduction this just sounds like you're running up against #1444

huskyrobotdog commented 1 year ago

against

Thanks, I probably got it. Types in static libraries are not the same as types from Rust sources.