Add __replace__ for dataclasses#17469
Merged
JelleZijlstra merged 26 commits intopython:masterfrom Jul 3, 2024
Merged
Conversation
for more information, see https://pre-commit.ci
…-muoto/mypy into add-dunder-replace-dataclass
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
Contributor
Author
|
CC: @hauntsaninja or @JelleZijlstra |
This comment has been minimized.
This comment has been minimized.
…-muoto/mypy into add-dunder-replace-dataclass
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@JelleZijlstra Testing an unexpected kwarg: bdfa9fb |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@JelleZijlstra Generics are working properly now. |
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra
approved these changes
Jul 3, 2024
|
|
||
| replaced_2 = Gen(2).__replace__(x=2) | ||
| reveal_type(replaced_2) # N: Revealed type is "__main__.Gen[builtins.int]" | ||
| Gen(2).__replace__(x="not an int") # E: Argument "x" to "__replace__" of "Gen" has incompatible type "str"; expected "int" |
Member
There was a problem hiding this comment.
I think theoretically we could make this not error and make it return a Gen[str], but that seems tricky to get right in general (what if there are multiple attributes?). We can omit it for now and add it later if people ask for it.
Contributor
Author
There was a problem hiding this comment.
Sounds good. I agree, I think for now it makes sense to keep like this to keep things simple, and we can revisit.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Add
__replace__, which is now present on dataclasses in 3.13, which is creating false alarms in TypeShed on dataclasses, and is needed forcopy.replaceto work properly.Fixes #17471