Skip to content

[3.13] gh-132111: Document dataclasses.InitVar (GH-132446) #132483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ Module contents
Other attributes may exist, but they are private and must not be
inspected or relied on.

.. class:: InitVar

``InitVar[T]`` type annotations describe variables that are :ref:`init-only
<dataclasses-init-only-variables>`. Fields annotated with :class:`!InitVar`
are considered pseudo-fields, and thus are neither returned by the
:func:`fields` function nor used in any way except adding them as
parameters to :meth:`~object.__init__` and an optional
:meth:`__post_init__`.

.. function:: fields(class_or_instance)

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

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