Skip to content

Commit 9a97e98

Browse files
committed
gh-139497: Clarify dataclass inheritance documentation
1 parent 701a7c5 commit 9a97e98

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Doc/library/dataclasses.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Module contents
194194
glossary entry for details. Also see the
195195
:const:`KW_ONLY` section.
196196

197+
This setting only applies to fields defined in the class being decorated;
198+
it does not affect inherited fields.
199+
197200
Keyword-only fields are not included in :attr:`!__match_args__`.
198201

199202
.. versionadded:: 3.10
@@ -682,6 +685,16 @@ must use :meth:`!object.__setattr__`.
682685
Inheritance
683686
-----------
684687

688+
The :deco:`dataclass` decorator is not automatically applied to subclasses.
689+
If a subclass is not itself decorated, no new dataclass fields are collected
690+
and no dataclass methods are generated for it. Instead, it inherits fields and
691+
methods through the normal method resolution order. This can be significant
692+
with multiple dataclass bases because their fields are not combined. Decorate
693+
the subclass to combine the fields of its dataclass bases as described below.
694+
Nevertheless, :func:`is_dataclass` considers an undecorated subclass of a
695+
dataclass to be a dataclass because it inherits dataclass metadata from a base
696+
class.
697+
685698
When the dataclass is being created by the :deco:`dataclass` decorator,
686699
it looks through all of the class's base classes in reverse MRO (that
687700
is, starting at :class:`object`) and, for each dataclass that it finds,

0 commit comments

Comments
 (0)