pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/821f0c8c39f522f28407bd0e8d4adeadd3e72776

stylesheet" href="https://github.githubassets.com/assets/global-d18f184ea1a06a2c.css" /> More work on documenting dataclass keyword-only fields. (GH-25828) · python/cpython@821f0c8 · GitHub
Skip to content

Commit 821f0c8

Browse files
authored
More work on documenting dataclass keyword-only fields. (GH-25828)
1 parent 555cbbe commit 821f0c8

1 file changed

Lines changed: 40 additions & 16 deletions

File tree

Doc/library/dataclasses.rst

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ directly specified in the ``InventoryItem`` definition shown above.
4343

4444
.. versionadded:: 3.7
4545

46-
Module-level decorators, classes, and functions
47-
-----------------------------------------------
46+
Module contents
47+
---------------
4848

4949
.. decorator:: dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False)
5050

@@ -177,7 +177,7 @@ Module-level decorators, classes, and functions
177177
with a keyword when :meth:`__init__` is called. There is no
178178
effect on any other aspect of dataclasses. See the
179179
:term:`parameter` glossary entry for details. Also see the
180-
``dataclasses.KW_ONLY`` section.
180+
:const:`KW_ONLY` section.
181181

182182
.. versionadded:: 3.10
183183

@@ -220,10 +220,10 @@ Module-level decorators, classes, and functions
220220
c = C()
221221
c.mylist += [1, 2, 3]
222222

223-
As shown above, the ``MISSING`` value is a sentinel object used to
223+
As shown above, the :const:`MISSING` value is a sentinel object used to
224224
detect if the ``default`` and ``default_factory`` parameters are
225225
provided. This sentinel is used because ``None`` is a valid value
226-
for ``default``. No code should directly use the ``MISSING``
226+
for ``default``. No code should directly use the :const:`MISSING`
227227
value.
228228

229229
The parameters to :func:`field` are:
@@ -433,6 +433,38 @@ Module-level decorators, classes, and functions
433433
def is_dataclass_instance(obj):
434434
return is_dataclass(obj) and not isinstance(obj, type)
435435

436+
.. data:: MISSING
437+
438+
A sentinel value signifying a missing default or default_factory.
439+
440+
.. data:: KW_ONLY
441+
442+
A sentinel value used a type annotation. Any fields after a
443+
pseudo-field with the type of :const:`KW_ONLY` are marked as
444+
keyword-only fields. Note that a pseudo-field of type
445+
:const:`KW_ONLY` is otherwise completely ignored. This includes the
446+
name of such a field. By convention, a name of `_` is used for a
447+
:const:`KW_ONLY` field. Keyword-only fields signify
448+
:meth:`__init__` parameters that must be specified as keywords when
449+
the class is instantiated.
450+
451+
In this example, the fields ``y`` and ``z`` will be marked as keyword-only fields::
452+
453+
@dataclass
454+
class Point:
455+
x: float
456+
_: KW_ONLY
457+
y: float
458+
z: float
459+
460+
p = Point(0, y=1.5, z=2.0)
461+
462+
.. exception:: FrozenInstanceError
463+
464+
Raised when an implicitly defined :meth:`__setattr__` or
465+
:meth:`__delattr__` is called on a dataclass which was defined with
466+
``frozen=True``. It is a subclass of :exc:`AttributeError`.
467+
436468
Post-init processing
437469
--------------------
438470

@@ -550,9 +582,9 @@ Re-ordering of keyword-only parameters in :meth:`__init__`
550582

551583
After the parameters needed for :meth:`__init__` are computed, any
552584
keyword-only parameters are moved to come after all regular
553-
(non-keyword-only) fields. In this example, ``Base.y``, ``Base.w``,
554-
and ``D.t`` are keyword-only fields, and ``Base.x`` and ``D.z`` are
555-
regular fields::
585+
(non-keyword-only) parameters. In this example, ``Base.y``,
586+
``Base.w``, and ``D.t`` are keyword-only fields, and ``Base.x`` and
587+
``D.z`` are regular fields::
556588

557589
@dataclass
558590
class Base:
@@ -652,11 +684,3 @@ Mutable default values
652684

653685
assert D().x is not D().x
654686

655-
Exceptions
656-
----------
657-
658-
.. exception:: FrozenInstanceError
659-
660-
Raised when an implicitly defined :meth:`__setattr__` or
661-
:meth:`__delattr__` is called on a dataclass which was defined with
662-
``frozen=True``. It is a subclass of :exc:`AttributeError`.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy