You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pyo3(get, set) attribute not found when using cfg_attr #1003
Based on #780 (comment) I wonder if we can resolve this by changing #[pyclass] expansion to (among other things) add a #[derive(PyClassImpl)] or something like that. Where the PyClassImpl derive macro would apparently be called with all cfg_attr attributes resolved.
This is kind of a hack, though, so I wonder if really we should be asking the compiler team if it's reasonable to expand all #[cfg_attr] clauses before calling custom attributes (as it already appears to do that for derive).
Is there a suggested workaround for this issue until that compiler feature lands? Thanks for the help!
You can manually implement getters/setters, and guard that block with a cfg:
#[cfg_attr(feature = "pyo3", pyclass)]
struct Foo{
bar: u8
}
#[cfg(feature = "pyo3")]
#[pymethods]
impl Foo{
// not named "bar" because there might already a rust getter for it
#[getter(bar)]
fn py_bar(&self) -> u8{
self.bar
}
}
I've had to do this for structs with hundreds of fields - not fun. Note that you may need to apply .clone() for fields that are not Copy.
Reacted by David Hewitt, Alexander Hirner and Chris
@mejrsget_all is awesome, thank you very much! If I hadn't scrolled until here, I would not have known. Maybe this should be documented somewhere more easy to find?
oh sorry, I thought the "pyo3(get,set) doesn't work with cfg_attr" problem was mentioned in the FAQs - if it is included in the future I'd recommend a sentence that suggests get_all as a workaround.
Activity
birkenfeld commentedon Jun 26, 2020
This is basically a duplicate of #780.
davidhewitt commentedon Aug 30, 2020
Based on #780 (comment) I wonder if we can resolve this by changing
#[pyclass]expansion to (among other things) add a#[derive(PyClassImpl)]or something like that. Where thePyClassImplderive macro would apparently be called with allcfg_attrattributes resolved.This is kind of a hack, though, so I wonder if really we should be asking the compiler team if it's reasonable to expand all
#[cfg_attr]clauses before calling custom attributes (as it already appears to do that for derive).davidhewitt commentedon Apr 23, 2021
Looks like the compiler team may indeed be doing that rust-lang/rust#83824
cgranade commentedon May 18, 2021
Is there a suggested workaround for this issue until that compiler feature lands? Thanks for the help!
mejrs commentedon May 19, 2021
You can manually implement getters/setters, and guard that block with a cfg:
I've had to do this for structs with hundreds of fields - not fun. Note that you may need to apply
.clone()for fields that are notCopy.Trolldemorted commentedon Apr 15, 2023
@mejrs
get_allis awesome, thank you very much! If I hadn't scrolled until here, I would not have known. Maybe this should be documented somewhere more easy to find?mejrs commentedon Apr 17, 2023
It is listed at https://pyo3.rs/v0.18.3/class.html#customizing-the-class I'm not sure where else this information should be. Where were you looking for it?
Trolldemorted commentedon Apr 21, 2023
oh sorry, I thought the "pyo3(get,set) doesn't work with cfg_attr" problem was mentioned in the FAQs - if it is included in the future I'd recommend a sentence that suggests
get_allas a workaround.hugohadfieldwayve commentedon Apr 27, 2023
For those stumbling upon this, here is a super explicit example with
get_allandcfg_attrRevert "remove cg-if dependency"