python / python/cpython

Add a new parameter to `@dataclass` to optionally turn off the synthesizing of a `__replace__` method

オープン
#140,457 コメント 5 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-feature
主要言語
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:

https://github.com/python/cpython/blob/a4709e525f5bc3a1719b33f52377cccb2af70278/Lib/dataclasses.py#L1098

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/dataclasses.py の replace が合成されるリンク先の行から始め、周辺の dataclass パラメータ処理を調べてください。replace=False の dataclass に replace が付与されず、デフォルトの動作は変更されない状態になれば完了です。必要に応じて、関連する dataclasses テストを追加または更新してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
機能追加
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。