MCPcopy
hub / github.com/python-attrs/attrs / evolve

Function evolve

src/attr/_make.py:587–642  ·  view source on GitHub ↗

Create a new instance, based on the first positional argument with *changes* applied. .. tip:: On Python 3.13 and later, you can also use `copy.replace` instead. Args: inst: Instance of a class with *attrs* attributes. *inst* must be passed

(*args, **changes)

Source from the content-addressed store, hash-verified

585
586
587def evolve(*args, **changes):
588 """
589 Create a new instance, based on the first positional argument with
590 *changes* applied.
591
592 .. tip::
593
594 On Python 3.13 and later, you can also use `copy.replace` instead.
595
596 Args:
597
598 inst:
599 Instance of a class with *attrs* attributes. *inst* must be passed
600 as a positional argument.
601
602 changes:
603 Keyword changes in the new copy.
604
605 Returns:
606 A copy of inst with *changes* incorporated.
607
608 Raises:
609 TypeError:
610 If *attr_name* couldn't be found in the class ``__init__``.
611
612 attrs.exceptions.NotAnAttrsClassError:
613 If *cls* is not an *attrs* class.
614
615 .. versionadded:: 17.1.0
616 .. deprecated:: 23.1.0
617 It is now deprecated to pass the instance using the keyword argument
618 *inst*. It will raise a warning until at least April 2024, after which
619 it will become an error. Always pass the instance as a positional
620 argument.
621 .. versionchanged:: 24.1.0
622 *inst* can't be passed as a keyword argument anymore.
623 """
624 try:
625 (inst,) = args
626 except ValueError:
627 msg = (
628 f"evolve() takes 1 positional argument, but {len(args)} were given"
629 )
630 raise TypeError(msg) from None
631
632 cls = inst.__class__
633 attrs = fields(cls)
634 for a in attrs:
635 if not a.init:
636 continue
637 attr_name = a.name # To deal with private attributes.
638 init_name = a.alias
639 if init_name not in changes:
640 changes[init_name] = getattr(inst, attr_name)
641
642 return cls(**changes)
643
644

Callers 10

test_emptyMethod · 0.90
test_no_changesMethod · 0.90
test_changeMethod · 0.90
test_unknownMethod · 0.90
test_privateMethod · 0.90
test_non_init_attrsMethod · 0.90
test_no_instMethod · 0.90
test_can_change_instMethod · 0.90

Calls 1

fieldsFunction · 0.85

Tested by 10

test_emptyMethod · 0.72
test_no_changesMethod · 0.72
test_changeMethod · 0.72
test_unknownMethod · 0.72
test_privateMethod · 0.72
test_non_init_attrsMethod · 0.72
test_no_instMethod · 0.72
test_can_change_instMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…