X Tutup
Skip to content

Commit 2f59a76

Browse files
authored
Improve the dataclasses kw_only documentation. (pythonGH-25799)
1 parent 29282b2 commit 2f59a76

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

Doc/library/dataclasses.rst

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ Module-level decorators, classes, and functions
169169
``__match_args__`` will not be generated.
170170

171171
- ``kw_only``: If true (the default value is ``False``), then all
172-
fields will be marked as keyword-only. See the :term:`parameter`
173-
glossary entry for details. Also see the ``dataclasses.KW_ONLY``
174-
section.
172+
fields will be marked as keyword-only. If a field is marked as
173+
keyword-only, then the only affect is that the :meth:`__init__`
174+
parameter generated from a keyword-only field must be specified
175+
with a keyword when :meth:`__init__` is called. There is no
176+
effect on any other aspect of dataclasses. See the
177+
:term:`parameter` glossary entry for details. Also see the
178+
``dataclasses.KW_ONLY`` section.
175179

176180
- ``slots``: If true (the default is ``False``), :attr:`__slots__` attribute
177181
will be generated and new class will be returned instead of the original one.
@@ -195,7 +199,7 @@ Module-level decorators, classes, and functions
195199
follows a field with a default value. This is true either when this
196200
occurs in a single class, or as a result of class inheritance.
197201

198-
.. function:: field(*, default=MISSING, default_factory=MISSING, repr=True, hash=None, init=True, compare=True, metadata=None)
202+
.. function:: field(*, default=MISSING, default_factory=MISSING, init=True, repr=True, hash=None, compare=True, metadata=None, kw_only=MISSING):
199203

200204
For common and simple use cases, no other functionality is
201205
required. There are, however, some dataclass features that
@@ -234,10 +238,6 @@ Module-level decorators, classes, and functions
234238
- ``repr``: If true (the default), this field is included in the
235239
string returned by the generated :meth:`__repr__` method.
236240

237-
- ``compare``: If true (the default), this field is included in the
238-
generated equality and comparison methods (:meth:`__eq__`,
239-
:meth:`__gt__`, et al.).
240-
241241
- ``hash``: This can be a bool or ``None``. If true, this field is
242242
included in the generated :meth:`__hash__` method. If ``None`` (the
243243
default), use the value of ``compare``: this would normally be
@@ -251,6 +251,10 @@ Module-level decorators, classes, and functions
251251
fields that contribute to the type's hash value. Even if a field
252252
is excluded from the hash, it will still be used for comparisons.
253253

254+
- ``compare``: If true (the default), this field is included in the
255+
generated equality and comparison methods (:meth:`__eq__`,
256+
:meth:`__gt__`, et al.).
257+
254258
- ``metadata``: This can be a mapping or None. None is treated as
255259
an empty dict. This value is wrapped in
256260
:func:`~types.MappingProxyType` to make it read-only, and exposed
@@ -259,6 +263,10 @@ Module-level decorators, classes, and functions
259263
Multiple third-parties can each have their own key, to use as a
260264
namespace in the metadata.
261265

266+
- ``kw_only``: If true, this field will be marked as keyword-only.
267+
This is used when the generated :meth:`__init__` method's
268+
parameters are computed.
269+
262270
If the default value of a field is specified by a call to
263271
:func:`field()`, then the class attribute for this field will be
264272
replaced by the specified ``default`` value. If no ``default`` is
@@ -532,15 +540,18 @@ The generated :meth:`__init__` method for ``C`` will look like::
532540
Re-ordering of keyword-only parameters in __init__
533541
--------------------------------------------------
534542

535-
After the fields needed for :meth:`__init__` are computed, any
536-
keyword-only fields are put after regular fields. In this example,
537-
``Base.y`` and ``D.t`` are keyword-only fields::
543+
After the parameters needed for :meth:`__init__` are computed, any
544+
keyword-only parameters are moved to come after regular
545+
(non-keyword-only) fields. In this example, ``Base.y``, ``Base.w``,
546+
and ``D.t`` are keyword-only fields, and ``Base.x`` and ``D.z`` are
547+
regular fields::
538548

539549
@dataclass
540550
class Base:
541551
x: Any = 15.0
542552
_: KW_ONLY
543553
y: int = 0
554+
w: int = 1
544555

545556
@dataclass
546557
class D(Base):
@@ -549,9 +560,13 @@ keyword-only fields are put after regular fields. In this example,
549560

550561
The generated :meth:`__init__` method for ``D`` will look like::
551562

552-
def __init__(self, x: Any = 15.0, z: int = 10, *, y: int = 0, t: int = 0):
563+
def __init__(self, x: Any = 15.0, z: int = 10, *, y: int = 0, w: int = 1, t: int = 0):
564+
565+
Note that the parameters have been re-ordered from how they appear in
566+
the list of fields: parameters derived from regular fields are
567+
followed by parameters derived from keyword-only fields.
553568

554-
The relative ordering of keyword-only arguments is not changed from
569+
The relative ordering of keyword-only parameters is not changed from
555570
the order they are in computed field :meth:`__init__` list.
556571

557572

0 commit comments

Comments
 (0)
X Tutup