PyO3 / pyo3

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

Allow module= attribute in complex enum variants #4228

Closed liammcinroy closed 4 months ago

liammcinroy commented 4 months ago

Allows use of the module attribute for complex enum variants (but not simple enums), like

#[pyclass(module = "custom_module"]
enum Complex {
    #[pyo3(module = "custom_module")]
    Variant(),
}

so that Complex.Variant.__module__ == "custom_module" instead of builtins

Useful to allow pickling (and introspection in general) for complex enums (as noted in https://github.com/PyO3/pyo3/issues/1517#issuecomment-806218369).

davidhewitt commented 4 months ago

Thanks for the PR! I wonder, is there any case where users want the module of the variant to be different to the whole enum?

My feeling is no, in which case maybe it's better if we propagate the module from the enum onto all the variants...

liammcinroy commented 4 months ago

@davidhewitt That was also my original feeling (at least, while the variants are still added to the same module as the whole enum). It is an even simpler PR then as well! latest commit has that change