Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively evolve nested attrs classes #759

Merged
merged 5 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/759.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Let ``evolve()`` work with nested ``attrs`` classes. #634
sscherfke marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,27 @@ In Clojure that function is called `assoc <https://clojuredocs.org/clojure.core/
>>> i1 == i2
False

This functions also works for nested ``attrs`` classes.
You just pass a (possibly nested) dict with changes for an attribute:
sscherfke marked this conversation as resolved.
Show resolved Hide resolved

.. doctest::

>>> @attr.s(frozen=True)
... class Child(object):
... x = attr.ib()
... y = attr.ib()
>>> @attr.s(frozen=True)
... class Parent(object):
... child = attr.ib()
>>> i1 = Parent(Child(1, 2))
>>> i1
Parent(child=Child(x=1, y=2))
>>> i2 = attr.evolve(i1, child={"y": 3})
>>> i2
Parent(child=Child(x=1, y=3))
>>> i1 == i2, i1.child == i2.child
(False, False)


Other Goodies
-------------
Expand Down
10 changes: 8 additions & 2 deletions src/attr/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ def evolve(inst, **changes):
Create a new instance, based on *inst* with *changes* applied.

:param inst: Instance of a class with ``attrs`` attributes.
:param changes: Keyword changes in the new copy.
:param changes: Keyword changes in the new copy. Nested attrs classes ca
be updated by passing (nested) dicts of values.
sscherfke marked this conversation as resolved.
Show resolved Hide resolved

:return: A copy of inst with *changes* incorporated.

Expand All @@ -337,8 +338,13 @@ def evolve(inst, **changes):
continue
attr_name = a.name # To deal with private attributes.
init_name = attr_name if attr_name[0] != "_" else attr_name[1:]
value = getattr(inst, attr_name)
if init_name not in changes:
changes[init_name] = getattr(inst, attr_name)
# Add original value to changes
changes[init_name] = value
elif has(value):
# Evolve nested attrs classes
changes[init_name] = evolve(value, **changes[init_name])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't that break the edge case of legitimately replacing a attrs instance with a dict where permissible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has(value) makes sure we only attempt recursion when the current value is an attrs class.

I added a test asserting that dicts can still normaly be replaced, nonetheless. :)


return cls(**changes)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,27 @@ class C(object):
b = attr.ib(init=False, default=0)

assert evolve(C(1), a=2).a == 2

def test_recursive(self):
"""
evolve() recursively evolves nested attrs classes when a dict is
passed for an attribute.
"""

@attr.s
class N2(object):
e = attr.ib(type=int)

@attr.s
class N1(object):
c = attr.ib(type=N2)
d = attr.ib(type=int)

@attr.s
class C(object):
a = attr.ib(type=N1)
b = attr.ib(type=int)

c1 = C(N1(N2(1), 2), 3)
c2 = evolve(c1, a={"c": {"e": 23}})
hynek marked this conversation as resolved.
Show resolved Hide resolved
assert c2 == C(N1(N2(23), 2), 3)
sscherfke marked this conversation as resolved.
Show resolved Hide resolved