diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-04-05 20:07:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-05 20:07:38 (GMT) |
commit | 013c30e5fcee449cee63354d34585d6111782c82 (patch) | |
tree | f820da2a47b1caed8ea5629528caab7113095033 /Lib/test/test_dataclasses.py | |
parent | 82cd24a03c09b93ed0bce0ad7a670d2e97c282c6 (diff) | |
download | cpython-013c30e5fcee449cee63354d34585d6111782c82.zip cpython-013c30e5fcee449cee63354d34585d6111782c82.tar.gz cpython-013c30e5fcee449cee63354d34585d6111782c82.tar.bz2 |
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867) (GH-25200)
Co-Authored-By: Claudiu Popa <pcmanticore@gmail.com>
Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit 75220674c07abfc90c2cd7862d04cfa2e2354450)
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r-- | Lib/test/test_dataclasses.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index b20103b..31caae9 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -3245,6 +3245,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: |