8000 gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests by adiaholic · Pull Request #119480 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119260: Clarify is_dataclass Behavior for Subclasses in Documentation and Tests #119480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
0