]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests...
authorAditya Borikar <adityaborikar2@gmail.com>
Wed, 29 May 2024 17:26:22 +0000 (11:26 -0600)
committerGitHub <noreply@github.com>
Wed, 29 May 2024 17:26:22 +0000 (17:26 +0000)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Doc/library/dataclasses.rst
Lib/test/test_dataclasses/__init__.py

index cf707ca5b6802df7968297163afc2326995d8c31..fcb5e8bad295a0e5c12e4f4202571782590deaf1 100644 (file)
@@ -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
index 04dd9f3265bb33e5def19a7d9dbcaa2dd0f94060..ffb8bbe75c504f24016e03d64fab74f6aff448d0 100644 (file)
@@ -1547,6 +1547,24 @@ class TestCase(unittest.TestCase):
         self.assertTrue(is_dataclass(type(a)))
         self.assertTrue(is_dataclass(a))
 
+    def test_is_dataclass_inheritance(self):
+        @dataclass
+        class X:
+            y: int
+
+        class Z(X):
+            pass
+
+        self.assertTrue(is_dataclass(X), "X should be a dataclass")
+        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",
+        )
 
     def test_helper_fields_with_class_instance(self):
         # Check that we can call fields() on either a class or instance,