def replace()

in chz/data_model.py [0:0]


def replace(obj: _T, /, **changes) -> _T:
    """Return a new object replacing specified fields with new values.

    Example:
    ```
    @chz.chz
    class Foo:
        a: int
        b: str

    foo = Foo(a=1, b="hello")
    assert chz.replace(foo, a=101) == Foo(a=101, b="hello")
    ```

    This just constructs a new object, so for example, the generated `__init__` gets run and
    validation will work exactly as if you manually constructed the new object.
    """
    if not hasattr(obj, "__chz_fields__"):
        raise ValueError(f"{obj} is not a chz object")

    for field in obj.__chz_fields__.values():
        if field.logical_name not in changes:
            changes[field.logical_name] = getattr(obj, field.x_name)
    return obj.__class__(**changes)