]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 30 May 2024 03:24:50 +0000 (05:24 +0200)
committerGitHub <noreply@github.com>
Thu, 30 May 2024 03:24:50 +0000 (03:24 +0000)
gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480)
(cherry picked from commit bf4ff3ad2e362801e87c85fffd9e140b774cef26)

Co-authored-by: Aditya Borikar <adityaborikar2@gmail.com>
Co-authored-by: Carl Meyer <carl@oddbird.net>
Doc/library/dataclasses.rst
Lib/test/test_dataclasses/__init__.py

index 455019d2206f95b0e03c982712038e523e5132db..e4a9cd4ebcbcab1065514648300f3611fb183240 100644 (file)
@@ -459,8 +459,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 c059726e9327e88d82f2f301766d9056a060ee2a..e15b34570efc4293fe3477de1d8bfadbf1972d53 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,