https://github.com/python/cpython/commit/681d7da144d1471ed5c0a155c838dc4a4a36636d commit: 681d7da144d1471ed5c0a155c838dc4a4a36636d branch: 3.12 author: Miss Islington (bot) <[email protected]> committer: carljm <[email protected]> date: 2024-05-30T03:24:50Z summary:
[3.12] gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480) (#119761) gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480) (cherry picked from commit bf4ff3ad2e362801e87c85fffd9e140b774cef26) Co-authored-by: Aditya Borikar <[email protected]> Co-authored-by: Carl Meyer <[email protected]> files: M Doc/library/dataclasses.rst M Lib/test/test_dataclasses/__init__.py diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst index 455019d2206f95..e4a9cd4ebcbcab 100644 --- a/Doc/library/dataclasses.rst +++ b/Doc/library/dataclasses.rst @@ -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 diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index c059726e9327e8..e15b34570efc42 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -1547,6 +1547,24 @@ class A(types.GenericAlias): 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, _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: [email protected]
