Skip to content

Commit

Permalink
[3.13] gh-119260: Clarify is_dataclass Behavior for Subclasses in Doc…
Browse files Browse the repository at this point in the history
…umentation and Tests (GH-119480) (#119760)

gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests (GH-119480)
(cherry picked from commit bf4ff3a)

Co-authored-by: Aditya Borikar <adityaborikar2@gmail.com>
Co-authored-by: Carl Meyer <carl@oddbird.net>
  • Loading branch information
3 people authored May 30, 2024
1 parent af57832 commit 06c62d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 06c62d6

Please sign in to comment.