-
-
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
Changes from all commits
b0a0190
df19ef3
6ab41c9
986c1fc
9a143a2
1be9150
2d45728
90cd81a
6b9f491
aaf1fd7
4e9a553
fda2699
dbb6dc3
5269ef9
16b0db8
e3f2b73
0338aee
bdfa9fb
2b4ea78
3291c48
b733e35
14b7ef6
56555c7
7422270
a1e11f1
30df8b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2489,3 +2489,37 @@ class Base: | |
class Child(Base): | ||
y: int | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDunderReplacePresent] | ||
# flags: --python-version 3.13 | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class Coords: | ||
x: int | ||
y: int | ||
|
||
|
||
replaced = Coords(2, 4).__replace__(x=2, y=5) | ||
reveal_type(replaced) # N: Revealed type is "__main__.Coords" | ||
|
||
replaced = Coords(2, 4).__replace__(x=2) | ||
reveal_type(replaced) # N: Revealed type is "__main__.Coords" | ||
|
||
Coords(2, 4).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "Coords" has incompatible type "str"; expected "int" | ||
Coords(2, 4).__replace__(23) # E: Too many positional arguments for "__replace__" of "Coords" | ||
Coords(2, 4).__replace__(23, 25) # E: Too many positional arguments for "__replace__" of "Coords" | ||
Coords(2, 4).__replace__(x=23, y=25, z=42) # E: Unexpected keyword argument "z" for "__replace__" of "Coords" | ||
|
||
from typing import Generic, TypeVar | ||
T = TypeVar('T') | ||
|
||
@dataclass | ||
class Gen(Generic[T]): | ||
x: T | ||
|
||
replaced_2 = Gen(2).__replace__(x=2) | ||
reveal_type(replaced_2) # N: Revealed type is "__main__.Gen[builtins.int]" | ||
max-muoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
[builtins fixtures/tuple.pyi] |
Uh oh!
There was an error while loading. Please reload this page.