PyO3 / PyO3/pyo3

declarative modules: #[pymodule_export] macro cannot be gated in `cfg_attr`

Open
#5,183 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

The following code fails to work properly because #[pymodule_export] gets ignored even if the pyo3 feature is enabled:

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

#[cfg_attr(feature = "pyo3", pymodule)]
pub mod py_const {
    #[cfg_attr(feature = "pyo3", pymodule_export)]
    pub const MY_CONST: () = ();
}

We should update the pymodule macro code to support cfg_attr and not blindly match on #[pymodule_export].

Issue initially reported by @SichangHe (I reused the example): https://github.com/PyO3/pyo3/issues/3900#issuecomment-2944213501

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the pymodule macro code and reproduce the issue with the Rust example and the pyo3 feature enabled. Trace how pymodule_export attributes are recognized through cfg_attr; done means the constant is exported when the feature is enabled without the direct attribute form.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.