summaryrefslogtreecommitdiff
path: root/Lib/test/test_dataclasses.py
diff options
context:
space:
mode:
authorEric V. Smith <ericvsmith@users.noreply.github.com>2021-11-22 08:26:12 -0500
committerGitHub <noreply@github.com>2021-11-22 08:26:12 -0500
commitd3062f672c92855b7e9e962ad4bf1a67abd4589b (patch)
tree6820af4d14c25fb50287439c821d86d4fa39f7c5 /Lib/test/test_dataclasses.py
parent5b946cadaa1175a5caae98bd2d309840bea52a58 (diff)
downloadcpython-git-d3062f672c92855b7e9e962ad4bf1a67abd4589b.tar.gz
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
Special handling is needed, because for non-slots dataclasses the instance attributes are not set: reading from a field just references the class's attribute of the same name, which contains the default value. But this doesn't work for classes using __slots__: they don't read the class's attribute. So in that case (and that case only), initialize the instance attribute. Handle this for both normal defaults, and for fields using default_factory.
Diffstat (limited to 'Lib/test/test_dataclasses.py')
-rw-r--r--Lib/test/test_dataclasses.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py
index b00d0484d3..bcd004f4ec 100644
--- a/Lib/test/test_dataclasses.py
+++ b/Lib/test/test_dataclasses.py
@@ -2880,6 +2880,28 @@ class TestSlots(unittest.TestCase):
self.assertIsNot(obj, p)
self.assertEqual(obj, p)
+ def test_slots_with_default_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default='b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
+ def test_slots_with_default_factory_no_init(self):
+ # Originally reported in bpo-44649.
+ @dataclass(slots=True)
+ class A:
+ a: str
+ b: str = field(default_factory=lambda:'b', init=False)
+
+ obj = A("a")
+ self.assertEqual(obj.a, 'a')
+ self.assertEqual(obj.b, 'b')
+
class TestDescriptors(unittest.TestCase):
def test_set_name(self):
# See bpo-33141.