[Python-checkins] More work on documenting dataclass keyword-only fields. (GH-25828)

ericvsmith webhook-mailer at python.org
Sun May 2 21:20:57 EDT 2021


https://github.com/python/cpython/commit/821f0c8c39f522f28407bd0e8d4adeadd3e72776
commit: 821f0c8c39f522f28407bd0e8d4adeadd3e72776
branch: master
author: Eric V. Smith <ericvsmith at users.noreply.github.com>
committer: ericvsmith <ericvsmith at users.noreply.github.com>
date: 2021-05-02T21:20:50-04:00
summary:

More work on documenting dataclass keyword-only fields. (GH-25828)

files:
M Doc/library/dataclasses.rst

diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst
index a27d09d0725de..1a7fed9af8187 100644
--- a/Doc/library/dataclasses.rst
+++ b/Doc/library/dataclasses.rst
@@ -43,8 +43,8 @@ directly specified in the ``InventoryItem`` definition shown above.
 
 .. versionadded:: 3.7
 
-Module-level decorators, classes, and functions
------------------------------------------------
+Module contents
+---------------
 
 .. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)
 
@@ -177,7 +177,7 @@ Module-level decorators, classes, and functions
      with a keyword when :meth:`__init__` is called.  There is no
      effect on any other aspect of dataclasses.  See the
      :term:`parameter` glossary entry for details.  Also see the
-     ``dataclasses.KW_ONLY`` section.
+     :const:`KW_ONLY` section.
 
     .. versionadded:: 3.10
 
@@ -220,10 +220,10 @@ Module-level decorators, classes, and functions
      c = C()
      c.mylist += [1, 2, 3]
 
-   As shown above, the ``MISSING`` value is a sentinel object used to
+   As shown above, the :const:`MISSING` value is a sentinel object used to
    detect if the ``default`` and ``default_factory`` parameters are
    provided.  This sentinel is used because ``None`` is a valid value
-   for ``default``.  No code should directly use the ``MISSING``
+   for ``default``.  No code should directly use the :const:`MISSING`
    value.
 
    The parameters to :func:`field` are:
@@ -433,6 +433,38 @@ Module-level decorators, classes, and functions
      def is_dataclass_instance(obj):
          return is_dataclass(obj) and not isinstance(obj, type)
 
+.. data:: MISSING
+
+   A sentinel value signifying a missing default or default_factory.
+
+.. data:: KW_ONLY
+
+   A sentinel value used a type annotation.  Any fields after a
+   pseudo-field with the type of :const:`KW_ONLY` are marked as
+   keyword-only fields.  Note that a pseudo-field of type
+   :const:`KW_ONLY` is otherwise completely ignored.  This includes the
+   name of such a field.  By convention, a name of `_` is used for a
+   :const:`KW_ONLY` field.  Keyword-only fields signify
+   :meth:`__init__` parameters that must be specified as keywords when
+   the class is instantiated.
+
+   In this example, the fields ``y`` and ``z`` will be marked as keyword-only fields::
+
+    @dataclass
+    class Point:
+      x: float
+      _: KW_ONLY
+      y: float
+      z: float
+
+    p = Point(0, y=1.5, z=2.0)
+
+.. exception:: FrozenInstanceError
+
+   Raised when an implicitly defined :meth:`__setattr__` or
+   :meth:`__delattr__` is called on a dataclass which was defined with
+   ``frozen=True``. It is a subclass of :exc:`AttributeError`.
+
 Post-init processing
 --------------------
 
@@ -550,9 +582,9 @@ Re-ordering of keyword-only parameters in :meth:`__init__`
 
 After the parameters needed for :meth:`__init__` are computed, any
 keyword-only parameters are moved to come after all regular
-(non-keyword-only) fields.  In this example, ``Base.y``, ``Base.w``,
-and ``D.t`` are keyword-only fields, and ``Base.x`` and ``D.z`` are
-regular fields::
+(non-keyword-only) parameters.  In this example, ``Base.y``,
+``Base.w``, and ``D.t`` are keyword-only fields, and ``Base.x`` and
+``D.z`` are regular fields::
 
   @dataclass
   class Base:
@@ -652,11 +684,3 @@ Mutable default values
 
      assert D().x is not D().x
 
-Exceptions
-----------
-
-.. exception:: FrozenInstanceError
-
-   Raised when an implicitly defined :meth:`__setattr__` or
-   :meth:`__delattr__` is called on a dataclass which was defined with
-   ``frozen=True``. It is a subclass of :exc:`AttributeError`.



More information about the Python-checkins mailing list