`#[pyclass]` trait ergonomics
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Following up to #4202 and #4206 I wanted to write down something I've been thinking about for a while. It looks like we're moving ahead with accepting those options so I'll assume that at least the design choice to have these options is accepted.
In summary, I think there are at least five convenience options to add to #[pyclass] to automatically expose Python magic methods based on Rust trait implementations:
-
#[pyclass(eq)]for equality based offPartialEq- #4210 -
#[pyclass(ord)]for ordering operators based offPartialOrd- #4202 -
#[pyclass(hash)]for hash based offHash- #4206 -
#[pyclass(str)]for__str__based offDisplay. I think we could also have#[pyclass(str = "<format>")]to specify a format-args-compatible string concisely. -
#[pyclass(repr)]for__repr__based offDebug. Similarly to the above I think we could have#[pyclass(repr = "<format>")].
Based off the recent PRs I think the basic implementation of these is more or less agreed upon, possibly excluding the extra argument on the string formatting options.
As we proceed with / make progress on the above, I think there are two further steps which make sense to explore:
- Simple enums currently generate
eqandreprautomatically. I think we should be deprecating the automatic generation and start requiring opt-in to the new behaviour, to be consistent with everything else. I think it's relatively easy to do this as we just need to emit the deprecation warning if theeqorreprarguments weren't passed. - I think a future extension would be to have
#[pyclass(init)]to automatically generate a constructor (like we already do for complex enums) and then#[pyclass(dataclass)]which does some or all of the above conveniences (maybe depending on whether the class isfrozen).
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 reading the related issues #4202, #4206, and #4210 to understand the accepted #[pyclass] option design. The remaining proposal concerns repr, deprecating automatic enum behavior, and possible init/dataclass extensions; work is done only after the scope and unresolved formatting choices are agreed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100