KEMBAR78
Do not include non-init fields in the synthesized `__replace__` method for dataclasses by Viicos · Pull Request #18221 · python/mypy · GitHub
Skip to content

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.

@github-actions
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
Member

@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
Member

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