Skip to content

Commit 281fc33

Browse files
authored
gh-132111: Document dataclasses.InitVar (#132446)
1 parent 1d97488 commit 281fc33

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Doc/library/dataclasses.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,15 @@ Module contents
344344
Other attributes may exist, but they are private and must not be
345345
inspected or relied on.
346346

347+
.. class:: InitVar
348+
349+
``InitVar[T]`` type annotations describe variables that are :ref:`init-only
350+
<dataclasses-init-only-variables>`. Fields annotated with :class:`!InitVar`
351+
are considered pseudo-fields, and thus are neither returned by the
352+
:func:`fields` function nor used in any way except adding them as
353+
parameters to :meth:`~object.__init__` and an optional
354+
:meth:`__post_init__`.
355+
347356
.. function:: fields(class_or_instance)
348357

349358
Returns a tuple of :class:`Field` objects that define the fields for this
@@ -600,8 +609,8 @@ Init-only variables
600609

601610
Another place where :func:`@dataclass <dataclass>` inspects a type annotation is to
602611
determine if a field is an init-only variable. It does this by seeing
603-
if the type of a field is of type ``dataclasses.InitVar``. If a field
604-
is an ``InitVar``, it is considered a pseudo-field called an init-only
612+
if the type of a field is of type :class:`InitVar`. If a field
613+
is an :class:`InitVar`, it is considered a pseudo-field called an init-only
605614
field. As it is not a true field, it is not returned by the
606615
module-level :func:`fields` function. Init-only fields are added as
607616
parameters to the generated :meth:`~object.__init__` method, and are passed to

0 commit comments

Comments
 (0)