Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not include non-init fields in the synthesized __replace__ method for dataclasses #18221

Merged
merged 2 commits into from
Dec 1, 2024

Conversation

Viicos
Copy link
Contributor

@Viicos Viicos commented Dec 1, 2024

At runtime, non init fields are not allowed when using replace. See the source code of the CPython implementation.

Copy link
Contributor

github-actions bot commented Dec 1, 2024

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@brianschubert brianschubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test, can we confirm that Coords(2, 4).__replace__(z=3) will issue an error?

@brianschubert
Copy link
Collaborator

Is that covered by this case a little further down?

Coords(2, 4).__replace__(x=23, y=25, z=42)  # E: Unexpected keyword argument "z" for "__replace__" of "Coords"

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it is, should have expanded the diff and looked closely :-)

@hauntsaninja hauntsaninja merged commit 9dad464 into python:master Dec 1, 2024
19 checks passed
@Viicos Viicos deleted the dataclass-replace-non-init branch December 2, 2024 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants