Skip to content

Commit aecd915

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

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

Doc/library/dataclasses.rst

Lines changed: 12 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,15 @@ 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. An
694+
undecorated subclass of a dataclass is nevertheless considered a dataclass by
695+
:func:`is_dataclass` because it inherits dataclass metadata from a base class.
696+
685697
When the dataclass is being created by the :deco:`dataclass` decorator,
686698
it looks through all of the class's base classes in reverse MRO (that
687699
is, starting at :class:`object`) and, for each dataclass that it finds,

0 commit comments

Comments
 (0)