PyO3 / PyO3/pyo3

Making docs accessible to Python only and / or Rust only.

Open
#5,269 6 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

At the moment if we have a function like the following:

/// A function to add two numbers together.
///
/// # Examples
///
/// ```rust
/// let x = sum(10, 12);
/// assert_eq!(x, 22);
/// ```
/// 
/// ``` python
/// >>> assert sum(10, 12) == 22
/// ```
///
/// # Errors
/// 
/// This function panics on overflow of `i32`.
#[pyfunction]
fn sum(x: i32, y: i32) -> i32 { ... }

Then we currently copy the whole docstring from Rust to Python. This is not ideal; it would be nice to be able to have a Rust-specific example which is only visible from rustdoc and a Python-specific example which is only visible from Python.

I think we might be able to solve this by having a #[pyo3(doc)] attribute which we can use to switch mode for consuming docs into the final form for each language.

I think it might be something like:

  • #[pyo3(doc = "rust")] - docs from this point onward are left in-place for Rustdoc and stripped from Python docstring
  • #[pyo3(doc = "py")] - docs from this point onward are stripped from Rustdoc and included in the Python docstring. I think we can do this by manipulating the expanded #[doc] attributes, but needs confirmation.
  • #[pyo3(doc = "both")] - docs from this point onwards are visible in both languages. This is the default mode all docs start in.

So the above might be written something like:

/// A function to add two numbers together.
///
/// # Examples
///
#[pyo3(doc = "rust")] // rust-only example
/// ```rust
/// let x = sum(10, 12);
/// assert_eq!(x, 22);
/// ```
///
#[pyo3(doc = "py")]  // python-only example
/// ``` python
/// >>> assert sum(10, 12) == 22
/// ```
///
#[pyo3(doc = "both")]  // show the errors section in both languages
/// # Errors
/// 
/// This function panics on overflow of `i32`.
#[pyfunction]
fn sum(x: i32, y: i32) -> i32 { ... }

What do folks think?

There might be better forms like #[pyo3(doc(rust))] / #[pyo3(doc(py))] etc. If the concept is proven to work we can bikeshed the syntax.

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 by tracing how PyO3 copies Rust doc comments into Python docstrings and how expanded #[doc] attributes are handled. Confirm whether language-specific sections can be removed from one generated form while remaining in the other. Done means a documented syntax supports rust-only, Python-only, and shared sections, with tests covering both generated documentation outputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.