gh-148941: Skip generating __init__ when class already defines it#151184
Open
nahcmon wants to merge 2 commits into
Open
gh-148941: Skip generating __init__ when class already defines it#151184nahcmon wants to merge 2 commits into
nahcmon wants to merge 2 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)'), |
Member
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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