-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add __replace__
for dataclasses
#17469
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
Add __replace__
for dataclasses
#17469
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
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.
@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.
@JelleZijlstra Generics are working properly now. |
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
|
||
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I agree, I think for now it makes sense to keep like this to keep things simple, and we can revisit.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Add
__replace__
, which is now present on dataclasses in 3.13, which is creating false alarms in TypeShed on dataclasses, and is needed forcopy.replace
to work properly.Fixes #17471