PyO3 / PyO3/pyo3

Automatically provide fallback constructor for all-public Rust structs

Open
#1,376 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In my application I have many "plain-old-data" structs, i.e. structs with only public fields that are supposed to be instantiated with the StructName { field1: ..., field2: ... } syntax in Rust.

Making those structs usable from Python-land currently involves a bit of avoidable boilerplate:

struct StructName {
    #[pyo3(get, set)] pub field1: u32,
    #[pyo3(get, set)] pub field1: u32,
}

// this is the boilerplate I'm talking about
#[pymethods]
impl StructName {
    #[new]
    pub fn new(field1: u32, field2: u32) -> Self {
        Self { field1, field2 }
    }
}

This kind of class is equivalent to Python's dataclasses. Python's dataclasses include default constructors which is very convenient and sensible. I think it would be nice for PyO3 to do the same and automatically provide default constructors like above, for plain-old-data structs with all pub fields.

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 handles the #[pymethods] and #[new] entry points for Rust structs. Determine the supported scope for detecting structs whose fields are all public, then verify that such structs receive a constructor matching their fields without the existing boilerplate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
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.