Skip to content

Commit 83aa496

Browse files
authored
gh-101100: Add reference doc for __post_init__ (#103818)
Signed-off-by: Olga Matoula <[email protected]>
1 parent c3453fb commit 83aa496

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

Doc/library/dataclasses.rst

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -437,19 +437,19 @@ Module contents
437437

438438
The newly returned object is created by calling the :meth:`~object.__init__`
439439
method of the dataclass. This ensures that
440-
:ref:`__post_init__ <post-init-processing>`, if present, is also called.
440+
:meth:`__post_init__`, if present, is also called.
441441

442442
Init-only variables without default values, if any exist, must be
443443
specified on the call to :func:`replace` so that they can be passed to
444-
:meth:`~object.__init__` and :ref:`__post_init__ <post-init-processing>`.
444+
:meth:`~object.__init__` and :meth:`__post_init__`.
445445

446446
It is an error for ``changes`` to contain any fields that are
447447
defined as having ``init=False``. A :exc:`ValueError` will be raised
448448
in this case.
449449

450450
Be forewarned about how ``init=False`` fields work during a call to
451451
:func:`replace`. They are not copied from the source object, but
452-
rather are initialized in :ref:`__post_init__ <post-init-processing>`, if they're
452+
rather are initialized in :meth:`__post_init__`, if they're
453453
initialized at all. It is expected that ``init=False`` fields will
454454
be rarely and judiciously used. If they are used, it might be wise
455455
to have alternate class constructors, or perhaps a custom
@@ -510,30 +510,31 @@ Module contents
510510
Post-init processing
511511
--------------------
512512

513-
The generated :meth:`~object.__init__` code will call a method named
514-
:meth:`!__post_init__`, if :meth:`!__post_init__` is defined on the
515-
class. It will normally be called as ``self.__post_init__()``.
516-
However, if any ``InitVar`` fields are defined, they will also be
517-
passed to :meth:`!__post_init__` in the order they were defined in the
518-
class. If no :meth:`~object.__init__` method is generated, then
519-
:meth:`!__post_init__` will not automatically be called.
513+
.. function:: __post_init__()
520514

521-
Among other uses, this allows for initializing field values that
522-
depend on one or more other fields. For example::
515+
When defined on the class, it will be called by the generated
516+
:meth:`~object.__init__`, normally as ``self.__post_init__()``.
517+
However, if any ``InitVar`` fields are defined, they will also be
518+
passed to :meth:`__post_init__` in the order they were defined in the
519+
class. If no :meth:`~object.__init__` method is generated, then
520+
:meth:`__post_init__` will not automatically be called.
523521

524-
@dataclass
525-
class C:
526-
a: float
527-
b: float
528-
c: float = field(init=False)
522+
Among other uses, this allows for initializing field values that
523+
depend on one or more other fields. For example::
529524

530-
def __post_init__(self):
531-
self.c = self.a + self.b
525+
@dataclass
526+
class C:
527+
a: float
528+
b: float
529+
c: float = field(init=False)
530+
531+
def __post_init__(self):
532+
self.c = self.a + self.b
532533

533534
The :meth:`~object.__init__` method generated by :func:`dataclass` does not call base
534535
class :meth:`~object.__init__` methods. If the base class has an :meth:`~object.__init__` method
535536
that has to be called, it is common to call this method in a
536-
:meth:`!__post_init__` method::
537+
:meth:`__post_init__` method::
537538

538539
@dataclass
539540
class Rectangle:
@@ -552,7 +553,7 @@ don't need to be called, since the derived dataclass will take care of
552553
initializing all fields of any base class that is a dataclass itself.
553554

554555
See the section below on init-only variables for ways to pass
555-
parameters to :meth:`!__post_init__`. Also see the warning about how
556+
parameters to :meth:`__post_init__`. Also see the warning about how
556557
:func:`replace` handles ``init=False`` fields.
557558

558559
Class variables
@@ -576,7 +577,7 @@ is an ``InitVar``, it is considered a pseudo-field called an init-only
576577
field. As it is not a true field, it is not returned by the
577578
module-level :func:`fields` function. Init-only fields are added as
578579
parameters to the generated :meth:`~object.__init__` method, and are passed to
579-
the optional :ref:`__post_init__ <post-init-processing>` method. They are not otherwise used
580+
the optional :meth:`__post_init__` method. They are not otherwise used
580581
by dataclasses.
581582

582583
For example, suppose a field will be initialized from a database, if a

0 commit comments

Comments
 (0)