ML Foundations

dataclasses.replace recomputes init-false fields

See why dataclasses.replace() reruns initialization and post-init logic instead of copying derived init=False fields.

Example results for dataclasses.replace recomputes init-false fields. original: Grid(width=3, area=900)
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

replace constructs a new dataclass through its initializer

`dataclasses.replace(instance, field=value)` returns a new object of the same dataclass type, copying the original init fields and applying the requested changes. It does not mutate the source instance. The function uses the class initializer, so normal `__post_init__` processing runs for the replacement. [1, 2]

This means replace behaves like constructing another instance from field values, not like copying every stored attribute byte-for-byte. Any derived value your `__post_init__` computes is recalculated from the replacement's inputs. [1]

References: [1], [2]

init-false fields are not copied from the source

A dataclass field declared with `init=False` is left out of the generated initializer. The documentation for `replace` says these fields are not copied from the source object; they are initialized by `__post_init__` when that method exists. The example deliberately makes the original grid's derived area stale, then changes its width with `replace`. [1, 2]

The replacement prints an area of 16 for width 4, while the original retains its deliberately changed area of 900. That result comes from the post-init calculation on the new object. This is useful for derived fields or caches that should follow constructor inputs instead of inheriting stale state. [1]

References: [1], [2]

Keep replace inputs to init fields

Pass values for fields that participate in initialization. An `init=False` field cannot be supplied as a normal `replace` change; the documentation specifies that doing so raises `ValueError`. To create a different derived value, change its source fields and calculate the result in `__post_init__`. [1]

The example checks both object identity and the two area values. It uses only an integer width and a derived integer area, so it does not claim behavior for nested mutable values, custom constructors, or side effects in post-init code. If your post-init logic depends on external state, consider whether constructing a replacement should repeat that work. [1, 2]

Executed with Python 3.12.14; assertions passed.

from dataclasses import dataclass, field, replace

@dataclass
class Grid:
    width: int
    area: int = field(init=False)

    def __post_init__(self):
        self.area = self.width * self.width

original = Grid(3)
original.area = 900  # simulate a stale derived cache
updated = replace(original, width=4)

print("original:", original)
print("replacement:", updated)
print("objects are distinct:", updated is not original)

assert original.area == 900
assert updated.area == 16
assert updated is not original

Recorded output

original: Grid(width=3, area=900)
replacement: Grid(width=4, area=16)
objects are distinct: True

References: [1], [2]

Sources

  1. dataclasses.replace
  2. dataclass post-init processing