File tree Expand file tree Collapse file tree 2 files changed +8
-2
lines changed Expand file tree Collapse file tree 2 files changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -400,7 +400,11 @@ def transform(self) -> bool:
400
400
401
401
def _add_dunder_replace (self , attributes : list [DataclassAttribute ]) -> None :
402
402
"""Add a `__replace__` method to the class, which is used to replace attributes in the `copy` module."""
403
- args = [attr .to_argument (self ._cls .info , of = "replace" ) for attr in attributes ]
403
+ args = [
404
+ attr .to_argument (self ._cls .info , of = "replace" )
405
+ for attr in attributes
406
+ if attr .is_in_init
407
+ ]
404
408
type_vars = [tv for tv in self ._cls .type_vars ]
405
409
add_method_to_class (
406
410
self ._api ,
Original file line number Diff line number Diff line change @@ -2492,12 +2492,14 @@ class Child(Base):
2492
2492
2493
2493
[case testDunderReplacePresent]
2494
2494
# flags: --python-version 3.13
2495
- from dataclasses import dataclass
2495
+ from dataclasses import dataclass, field
2496
2496
2497
2497
@dataclass
2498
2498
class Coords:
2499
2499
x: int
2500
2500
y: int
2501
+ # non-init fields are not allowed with replace:
2502
+ z: int = field(init=False)
2501
2503
2502
2504
2503
2505
replaced = Coords(2, 4).__replace__(x=2, y=5)
You can’t perform that action at this time.
0 commit comments