Sitelet https://web.archive.org/web/20260802133823/https://github.com/python/cpython/pull/107247
Skip to content

gh-91507: [dataclasses] preserve the order of __post_init__ kwargs - #107247

Open
szobov wants to merge 3 commits into
python:mainfrom
szobov:gh-91507-correct-order-of-post-init-args
Open

gh-91507: [dataclasses] preserve the order of __post_init__ kwargs#107247
szobov wants to merge 3 commits into
python:mainfrom
szobov:gh-91507-correct-order-of-post-init-args

Conversation

@szobov

@szobov szobov commented Jul 25, 2023 •

Copy link
Copy Markdown

Scope

These changes fix the problem described in #91507.
The issue was marked as closed but not solved and contains a very relevant discussion.

In-depth

  • First commit implements a test to show the problem. The arguments are passed as kwargs, but the order is ignored, and the values are assigned unexpectedly.
  • Second commit with the fix. After reading a discussion on the issue, both options from the @ericvsmith's comment sounded a little bit hacky. Therefore I decided to propose another solution that keeps the compatibility intact. The solution is the change __init__ method to accept *args, **kwargs and move the content of the old implementation to a new __main_init__.
  • Last commit fixes most of the tests. I didn't fix all tests before I understood the proposed solution was appropriate. I decided to fix most of them to highlight the interface changes.

Side note

Thanks for any guidance and advice.
I'm free to implement suggestions or close this PR if the proposed solution is unacceptable.
I'll sign CLA right after a review.

Best wishes,
Sergei

@bedevere-bot

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@ghost

ghost commented Jul 25, 2023

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

@ssweber

ssweber commented Jul 25, 2023

Copy link
Copy Markdown

@szobov I’m not at a computer to test. Does this work for a def like:

@dataclass
class Foo:
    x: InitVar[int]
    y: InitVar[int]
    def __post_init__(self, my_x, my_y):
        print(my_x, my_y)

@szobov

szobov commented Jul 26, 2023 •

Copy link
Copy Markdown
Author

Hello @ssweber!
Thanks for your comment.
Yes, the code snippet you mention works with the proposed implementation.

Here the test I used to test it:

    def test_post_init_different_params_names(self):

        @dataclass
        class Foo:
            x: InitVar[int]
            y: InitVar[int]
            expected_field_42: int = None

            def __post_init__(self, my_x, my_y):
                assert my_x == 42
                assert my_y == 24
                self.expected_field_42 = my_x

        foo = Foo(42, 24)
        self.assertEqual(foo.expected_field_42, 42)

Hope it addresses your concerns.
Looking forward to hearing back from you.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants