(obj, /, **changes)
return obj.__class__(**changes)
Return a new object replacing specified fields with new values.

    This is especially useful for frozen classes.  Example usage:

      @dataclass(frozen=True)
      class C:
          x: int
          y: int

      c = C(1, 2)
      c1 = replace(c, x=3)
      assert c1.x == 3 and c1.y == 2
Return a new object replacing specified fields with new values.
Return a new object replacing specified fields with new values .
def replace(obj, /, **changes):
    """Return a new object replacing specified fields with new values.

    This is especially useful for frozen classes.  Example usage:

      @dataclass(frozen=True)
      class C:
          x: int
          y: int

      c = C(1, 2)
      c1 = replace(c, x=3)
      assert c1.x == 3 and c1.y == 2
      """

    # We're going to mutate 'changes', but that's okay because it's a
    # new dict, even if called with 'replace(obj, **my_changes)'.

    if not _is_dataclass_instance(obj):
        raise TypeError("replace() should be called on dataclass instances")

    # It's an error to have init=False fields in 'changes'.
    # If a field is not in 'changes', read its value from the provided obj.

    for f in getattr(obj, _FIELDS).values():
        # Only consider normal fields or InitVars.
        if f._field_type is _FIELD_CLASSVAR:
            continue

        if not f.init:
            # Error if this field is specified in changes.
            if f.name in changes:
                raise ValueError(f'field {f.name} is declared with '
                                 'init=False, it cannot be specified with '
                                 'replace()')
            continue

        if f.name not in changes:
            if f._field_type is _FIELD_INITVAR and f.default is MISSING:
                raise ValueError(f"InitVar {f.name!r} "
                                 'must be specified with replace()')
            changes[f.name] = getattr(obj, f.name)

    # Create the new object, which calls __init__() and
    # __post_init__() (if defined), using all of the init fields we've
    # added and/or left in 'changes'.  If there are values supplied in
    # changes that aren't fields, this will correctly raise a
    # TypeError.
    return obj.__class__(**changes)
def replace ( obj , / , ** changes ) : # We're going to mutate 'changes', but that's okay because it's a # new dict, even if called with 'replace(obj, **my_changes)'. if not _is_dataclass_instance ( obj ) : raise TypeError ( "replace() should be called on dataclass instances" ) # It's an error to have init=False fields in 'changes'. # If a field is not in 'changes', read its value from the provided obj. for f in getattr ( obj , _FIELDS ) . values ( ) : # Only consider normal fields or InitVars. if f . _field_type is _FIELD_CLASSVAR : continue if not f . init : # Error if this field is specified in changes. if f . name in changes : raise ValueError ( f'field {f.name} is declared with ' 'init=False, it cannot be specified with ' 'replace()' ) continue if f . name not in changes : if f . _field_type is _FIELD_INITVAR and f . default is MISSING : raise ValueError ( f"InitVar {f.name!r} " 'must be specified with replace()' ) changes [ f . name ] = getattr ( obj , f . name ) # Create the new object, which calls __init__() and # __post_init__() (if defined), using all of the init fields we've # added and/or left in 'changes'.  If there are values supplied in # changes that aren't fields, this will correctly raise a # TypeError. return obj . __class__ ( * * changes )