Add a new parameter to `@dataclass` to optionally turn off the synthesizing of a `__replace__` method
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
Most (all?) synthesized methods on dataclasses can be turned off:
__init__->init=False__repr__->repr=False__eq__->eq=False__match_args__->match_args=False__slots__->slots=False__lt__(),__le__(),__gt__(),__ge__()->order=False
But __replace__ cannot be turned off, which presents a problem for generic dataclasses: https://discuss.python.org/t/make-replace-stop-interfering-with-variance-inference/96092/
I propose this
@dataclass(replace=False)
class D:
x: int
to create a dataclass without __replace__ method.
As far as I can tell, all that's needed is to put a guard around this line:
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
I proposed it in this comment on the DPO thread: https://discuss.python.org/t/make-replace-stop-interfering-with-variance-inference/96092/19
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 in Lib/dataclasses.py at the linked line where replace is synthesized, and inspect the surrounding dataclass parameter handling. Done means a replace=False dataclass does not receive replace, while the default behavior remains unchanged; add or update the relevant dataclasses tests as needed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100