Add a new parameter to `@dataclass` to optionally turn off the synthesizing of a `__replace__` method
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/dataclasses.py 中合成 replace 的链接行开始,检查周围的 dataclass 参数处理。完成标准是:replace=False 的 dataclass 不会获得 replace,同时默认行为保持不变;根据需要添加或更新相关的 dataclasses 测试。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 52/100