dataclass tries to generate `__init__()` even though the class already defines one
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
When running this code:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
I got TypeError: non-default argument 'b' follows default argument
However, if I set init=False for the second dataclass, like this:
from dataclasses import dataclass
@dataclass
class A:
a: int = 1
@dataclass(init=False)
class B(A):
b: int
def __init__(self, *args, **kwargs): ...
There error disappeared.
I think the error comes from the process where dataclass tries to generate the default __init__() and checks the fields. But, if I understand correctly, this should not happen when the class already defines its own __init__().
Although I'm not sure whether the behavior should be defined as a bug, I do find the results inconsistant with the docs, which says
If the class already defines
__init__(), this parameter (init) is ignored.
so, intuitively, adding init=False shouldn't have changed the result.
CPython versions tested on:
3.12, 3.13, 3.14
Operating systems tested on:
Windows
Linked PRs
- gh-151184
- gh-153601
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
dataclasses.dataclass を使用して 2 つの例を再現し、その後、動作またはドキュメントに一貫性がないかどうかを判断する前に、gh-151184 と gh-153601 の関連する作業を調査します。既存の init の意図された扱いが確立され、適切なリグレッションの証拠でカバーされていれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100