Skip to content

Commit d021b71

Browse files
[3.13] gh-132111: Document dataclasses.InitVar (GH-132446) (#132483)
gh-132111: Document dataclasses.InitVar (GH-132446) (cherry picked from commit 281fc33) Co-authored-by: Tapeline <[email protected]>
1 parent 54e1b63 commit d021b71

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
@@ -347,6 +347,15 @@ Module contents
347347
Other attributes may exist, but they are private and must not be
348348
inspected or relied on.
349349

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

352361
Returns a tuple of :class:`Field` objects that define the fields for this
@@ -595,8 +604,8 @@ Init-only variables
595604

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

0 commit comments

Comments
 (0)