Attribute macro confusion in pyO3

Trying to make code with an optional feature "pysomething". I've just now stumbled onto cfg_attr, and it works for simple attribute macros in pyo3, but I'm having trouble with pyo3(get):

If I build this code with the "pysomething" feature enabled, it compiles, but, obviously, doesn't compile with the "pysomething" feature disabled.

#[cfg(feature = "pysomething")]
use pyo3::prelude::*;

#[cfg_attr(feature = "pysomething", pyclass)]
pub struct My_Struct {
    #[pyo3(get, set)]
    pub foo: bool,
}

If I build this code with the "pysomething" feature enabled, it doesn't compile, with the error cannot find attribute `pyo3` in this scope , but, it does compile with the "pysomething" feature disabled.

#[cfg(feature = "pysomething")]
use pyo3::prelude::*;

#[cfg_attr(feature = "pysomething", pyclass)]
pub struct My_Struct {
    #[cfg_attr(feature = "pysomething", pyo3(get, set))]
    pub foo: bool,
}

I'm assuming this has something to do with the way I'm using the pyo3(get, set) macro, or it has something to do with using macros that take arguments inside of a cfg_attr macro.

So, um -- help? I think it's obvious what I'm trying to do.

This is a known, unfortunate issue related to how the compiler parses attributes. See here and here. I gather that rustc is going to learn how to handle this situation more gracefully, but the relevant features have not landed/stabilized yet. Those issue threads have some discussion of workarounds.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.