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

Method evolve

src/attr/_make.py:2596–2625  ·  view source on GitHub ↗

Copy *self* and apply *changes*. This works similarly to `attrs.evolve` but that function does not work with :class:`attrs.Attribute`. It is mainly meant to be used for `transform-fields`. .. versionadded:: 20.3.0

(self, **changes)

Source from the content-addressed store, hash-verified

2594
2595 # Don't use attrs.evolve since fields(Attribute) doesn't work
2596 def evolve(self, **changes):
2597 """
2598 Copy *self* and apply *changes*.
2599
2600 This works similarly to `attrs.evolve` but that function does not work
2601 with :class:`attrs.Attribute`.
2602
2603 It is mainly meant to be used for `transform-fields`.
2604
2605 .. versionadded:: 20.3.0
2606 """
2607 new = copy.copy(self)
2608
2609 new._setattrs(changes.items())
2610
2611 if "alias" in changes and "alias_is_default" not in changes:
2612 # Explicit alias provided -- no longer the default.
2613 _OBJ_SETATTR.__get__(new)("alias_is_default", False)
2614 elif (
2615 "name" in changes
2616 and "alias" not in changes
2617 # Don't auto-generate alias if the user picked picked the old one.
2618 and self.alias_is_default
2619 ):
2620 # Name changed, alias was auto-generated -- update it.
2621 _OBJ_SETATTR.__get__(new)(
2622 "alias", _default_init_alias_for(new.name)
2623 )
2624
2625 return new
2626
2627 # Don't use _add_pickle since fields(Attribute) doesn't work
2628 def __getstate__(self):

Callers 10

_collect_base_attrsFunction · 0.80
_transform_attrsFunction · 0.80
test_evolveMethod · 0.80
hookMethod · 0.80
use_dataclass_namesMethod · 0.80
test_dictsMethod · 0.80
baseline.pyFile · 0.80
mypy.pyFile · 0.80

Calls 2

_default_init_alias_forFunction · 0.85
_setattrsMethod · 0.80

Tested by 5

test_evolveMethod · 0.64
hookMethod · 0.64
use_dataclass_namesMethod · 0.64
test_dictsMethod · 0.64