python-attrs / python-attrs/attrs
Recursive attr.evolve
Open
@sscherfke is already working on this.
Since Oct 30, 2020.
Feature
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
In the same manner as attr.asdict(..., recursive=True) exists, I have a use case for a similar option to attr.evolve(). I want all attr instances evolved, but with no need to deepcopy any non-attr attributes. I ended up implementing the code below. Could this be a consideration for addition into python-attrs?
def evolve_recursive(inst, **changes):
""" Recursive attr.evolve() method, where any attr-based attributes
will be evolved too.
"""
cls = inst.__class__
attrs = attr.fields(cls)
for a in attrs:
if not a.init:
continue
attr_name = a.name # To deal with private attributes.
init_name = attr_name if attr_name[0] != "_" else attr_name[1:]
if init_name not in changes:
value = getattr(inst, attr_name)
# New code
if attr.has(value.__class__):
value = evolve_recursive(value)
changes[init_name] = value
return cls(**changes)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.