@@ -437,19 +437,19 @@ Module contents
437
437
438
438
The newly returned object is created by calling the :meth: `~object.__init__ `
439
439
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.
441
441
442
442
Init-only variables without default values, if any exist, must be
443
443
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__ `.
445
445
446
446
It is an error for ``changes `` to contain any fields that are
447
447
defined as having ``init=False ``. A :exc: `ValueError ` will be raised
448
448
in this case.
449
449
450
450
Be forewarned about how ``init=False `` fields work during a call to
451
451
: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
453
453
initialized at all. It is expected that ``init=False `` fields will
454
454
be rarely and judiciously used. If they are used, it might be wise
455
455
to have alternate class constructors, or perhaps a custom
@@ -510,30 +510,31 @@ Module contents
510
510
Post-init processing
511
511
--------------------
512
512
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__()
520
514
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.
523
521
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::
529
524
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
532
533
533
534
The :meth: `~object.__init__ ` method generated by :func: `dataclass ` does not call base
534
535
class :meth: `~object.__init__ ` methods. If the base class has an :meth: `~object.__init__ ` method
535
536
that has to be called, it is common to call this method in a
536
- :meth: `! __post_init__ ` method::
537
+ :meth: `__post_init__ ` method::
537
538
538
539
@dataclass
539
540
class Rectangle:
@@ -552,7 +553,7 @@ don't need to be called, since the derived dataclass will take care of
552
553
initializing all fields of any base class that is a dataclass itself.
553
554
554
555
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
556
557
:func: `replace ` handles ``init=False `` fields.
557
558
558
559
Class variables
@@ -576,7 +577,7 @@ is an ``InitVar``, it is considered a pseudo-field called an init-only
576
577
field. As it is not a true field, it is not returned by the
577
578
module-level :func: `fields ` function. Init-only fields are added as
578
579
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
580
581
by dataclasses.
581
582
582
583
For example, suppose a field will be initialized from a database, if a
0 commit comments