gh-148941: Skip generating __init__ when class already defines it#151184
gh-148941: Skip generating __init__ when class already defines it#151184nahcmon wants to merge 3 commits into
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
| (f' if {condition}:', | ||
| ' raise FrozenInstanceError(f"cannot assign to field {name!r}")', | ||
| f' super(__class__, self).__setattr__(name, value)'), | ||
| ' super(__class__, self).__setattr__(name, value)'), |
There was a problem hiding this comment.
Please revert all unrelated changes.
…it (pythonGH-148941) When @DataClass(init=True) is applied to a class that already defines __init__ in its own __dict__, the generated __init__ is always discarded by _set_new_attribute. However, _init_fn was still called unconditionally, and its field-ordering validation raised TypeError for inherited fields with defaults followed by fields without. The docs state "If the class already defines __init__(), this parameter is ignored." Align the implementation with that documented behaviour by checking '__init__' not in cls.__dict__ before calling _init_fn.
01401bd to
8429874
Compare
…-when-user-defined
|
I don't think we should make this change. Certainly not in isolation like this. Currently all of the main methods are only checked after generation when they are added to the class via the Either we should keep the status quo of only checking after generation or we should check all of the methods on the class before generating. I'm also not certain we shouldn't treat this as an error if the user hasn't declared |
Bug
When
@dataclass(init=True)(the default) is applied to a class that already defines__init__in its own__dict__, the generated__init__is always discarded by_set_new_attribute. However,_init_fnwas still called unconditionally, and its field-ordering validation raisedTypeErrorwhen inherited fields with defaults were followed by fields without defaults:The docs state: "If the class already defines
__init__(), this parameter is ignored." The design table indataclasses.pyitself also documents this as the intended behaviour (theinit=True/ class-has-__init__cell is intentionally blank/no-op).Fix
Add
and '__init__' not in cls.__dict__to the guard before calling_init_fnin_process_class. This skips the generation (and its validation) entirely when the class's own__init__would have been preserved anyway.Tests
test_overwriting_init_with_field_ordering_conflictis added toTestInitinLib/test/test_dataclasses/__init__.py:TypeError: non-default argument 'y' follows default argument 'x'Child(5).y == 5, class's own__init__is usedAll 282 dataclasses tests pass.
NEWS
Misc/NEWS.d/next/Library/2026-06-09-16-41-45.gh-issue-148941.aB3kLm.rstadded.CLA
nahcmonmay not have a signed PSF CLA on file. If the CLA bot flags this PR, the contributor will need to sign at https://www.python.org/psf/contrib/contrib-form/ before it can be merged.__init__()even though the class already defines one #148941