From cdb0e494e5e107b6049a3811c4bb314db75186d0 Mon Sep 17 00:00:00 2001 From: Aditya Borikar Date: Tue, 28 May 2024 14:15:43 -0600 Subject: [PATCH 1/3] Clarify is_dataclass Behavior for Subclasses in Documentation and Tests --- Doc/library/dataclasses.rst | 4 ++-- Lib/test/test_dataclasses/__init__.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index cf707ca5b6802d..c93a0b97d4cd18 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -461,8 +461,8 @@ Module contents .. function:: is_dataclass(obj) - Return ``True`` if its parameter is a dataclass or an instance of one, - otherwise return ``False``. + Return ``True`` if its parameter is a dataclass (including subclasses of a dataclass) + or an instance of one, otherwise return ``False``. If you need to know if a class is an instance of a dataclass (and not a dataclass itself), then add a further check for ``not diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index ea49596eaa4d96..03024ec4106f6c 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -1524,6 +1524,21 @@ class A(types.GenericAlias): self.assertTrue(is_dataclass(type(a))) self.assertTrue(is_dataclass(a)) + def test_dataclass_inheritance(self): + @dataclass + class X: + y: int + + class Z(X): + pass + + # Check if X is a dataclass + self.assertTrue(is_dataclass(X), "X should be a dataclass") + # Check if Z is a dataclass, it should inherit the dataclass behavior from X + self.assertTrue(is_dataclass(Z), "Z should be a dataclass because it inherits from X") + # Create an instance of Z and check if the default values are set correctly + z_instance = Z(y=5) + self.assertTrue(is_dataclass(z_instance), "z_instance should be a dataclass because it is an instance of Z") def test_helper_fields_with_class_instance(self): # Check that we can call fields() on either a class or instance, From 05d17d914e2c2b03e1bc36b6084663e4af2b493b Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 28 May 2024 15:33:55 -0600 Subject: [PATCH 2/3] Apply suggestions from code review --- Lib/test/test_dataclasses/__init__.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 03024ec4106f6c..d52fc07bd3815a 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -1524,7 +1524,7 @@ class A(types.GenericAlias): self.assertTrue(is_dataclass(type(a))) self.assertTrue(is_dataclass(a)) - def test_dataclass_inheritance(self): + def test_is_dataclass_inheritance(self): @dataclass class X: y: int @@ -1532,13 +1532,16 @@ class X: class Z(X): pass - # Check if X is a dataclass self.assertTrue(is_dataclass(X), "X should be a dataclass") - # Check if Z is a dataclass, it should inherit the dataclass behavior from X - self.assertTrue(is_dataclass(Z), "Z should be a dataclass because it inherits from X") - # Create an instance of Z and check if the default values are set correctly + self.assertTrue( + is_dataclass(Z), + "Z should be a dataclass because it inherits from X", + ) z_instance = Z(y=5) - self.assertTrue(is_dataclass(z_instance), "z_instance should be a dataclass because it is an instance of Z") + self.assertTrue( + is_dataclass(z_instance), + "z_instance should be a dataclass because it is an instance of Z", + ) def test_helper_fields_with_class_instance(self): # Check that we can call fields() on either a class or instance, From 012be20e26e3a5c7f614eed078c8a9475e39d615 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 28 May 2024 15:35:10 -0600 Subject: [PATCH 3/3] Update Doc/library/dataclasses.rst --- Doc/library/dataclasses.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index c93a0b97d4cd18..fcb5e8bad295a0 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -461,8 +461,8 @@ Module contents .. function:: is_dataclass(obj) - Return ``True`` if its parameter is a dataclass (including subclasses of a dataclass) - or an instance of one, otherwise return ``False``. + Return ``True`` if its parameter is a dataclass (including subclasses of a + dataclass) or an instance of one, otherwise return ``False``. If you need to know if a class is an instance of a dataclass (and not a dataclass itself), then add a further check for ``not