mirror of
https://github.com/python/cpython.git
synced 2025-08-27 12:16:04 +00:00
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867)
Co-Authored-By: Claudiu Popa <pcmanticore@gmail.com> Automerge-Triggered-By: GH:ericvsmith
This commit is contained in:
parent
14829b09eb
commit
75220674c0
3 changed files with 21 additions and 1 deletions
|
@ -3251,6 +3251,24 @@ class TestReplace(unittest.TestCase):
|
|||
c = replace(c, x=3, y=5)
|
||||
self.assertEqual(c.x, 15)
|
||||
|
||||
def test_initvar_with_default_value(self):
|
||||
@dataclass
|
||||
class C:
|
||||
x: int
|
||||
y: InitVar[int] = None
|
||||
z: InitVar[int] = 42
|
||||
|
||||
def __post_init__(self, y, z):
|
||||
if y is not None:
|
||||
self.x += y
|
||||
if z is not None:
|
||||
self.x += z
|
||||
|
||||
c = C(x=1, y=10, z=1)
|
||||
self.assertEqual(replace(c), C(x=12))
|
||||
self.assertEqual(replace(c, y=4), C(x=12, y=4, z=42))
|
||||
self.assertEqual(replace(c, y=4, z=1), C(x=12, y=4, z=1))
|
||||
|
||||
def test_recursive_repr(self):
|
||||
@dataclass
|
||||
class C:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue