Structs impling Default should have default Python constructors
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Currently pyo3 bindings require you to explicitly annotate any Rust "constructor" as a Python constructor. This means that if your struct relies on its default() method from the Default trait you cannot expose the default() method directly. Instead you have to do this:
#[pymethods]
impl MyStruct {
#[new]
fn new() -> Self {
Self::default()
}
}
In my opinion, exposing this default() method is a common enough problem that this boilerplate is a little unnecessary. What if instead we could do
#[derive(Default)]
#[pyclass(default)]
struct MyStruct;
Here the pyclass(default) would create a zero-argument constructor method for the Python type, and its implementation would call MyStruct::default.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the #[pyclass] handling and the existing #[pymethods] constructor behavior described in the issue. Verify how a #[pyclass(default)] option could call the Rust Default implementation and expose a zero-argument Python constructor; done means the proposed annotation works for a #[derive(Default)] struct and preserves current constructor behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100