Skip to content

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

Merged
merged 26 commits into from
Jul 3, 2024

Conversation

max-muoto
Copy link
Contributor

@max-muoto max-muoto commented Jul 3, 2024

Add __replace__, which is now present on dataclasses in 3.13, which is creating false alarms in TypeShed on dataclasses, and is needed for copy.replace to work properly.

Fixes #17471

@max-muoto max-muoto marked this pull request as draft July 3, 2024 04:47

This comment has been minimized.

@max-muoto max-muoto marked this pull request as ready for review July 3, 2024 06:44
@max-muoto
Copy link
Contributor Author

CC: @hauntsaninja or @JelleZijlstra

This comment has been minimized.

@max-muoto max-muoto marked this pull request as draft July 3, 2024 06:50

This comment has been minimized.

This comment has been minimized.

@max-muoto max-muoto marked this pull request as ready for review July 3, 2024 07:47
@max-muoto
Copy link
Contributor Author

@JelleZijlstra Testing an unexpected kwarg: bdfa9fb

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@max-muoto
Copy link
Contributor Author

@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"
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

github-actions bot commented Jul 3, 2024

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

@JelleZijlstra JelleZijlstra merged commit cb7b96d into python:master Jul 3, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dataclasses should define __replace__ in 3.13
2 participants