Skip to content

attr.evolve does not handle custom __init__s #207

Open
@Julian

Description

@Julian

attr.assoc appears to have been deprecated in #169 and is now scheduled for removal in favor of attr.evolve, but the latter appears to not be able to do all that the former can do (and the documentation seems to indicate that this gap is intentional).

Is there then a reason to deprecate the former, it seems like it should stay, or the functionality should go somewhere else? I can think of workarounds to change the way the below works, but want to make sure it's being broken intentionally -- as-is, it does work correctly with attr.assoc.

Sample code:

import attr
@attr.s(init=False)
class F(object):
    args = attr.ib()
    other = attr.ib()

    def __init__(self, *args, **kwargs):
        self.args = args
        self.other = kwargs.pop("other")
        if kwargs:
            raise TypeError(kwargs)

    def assoc_new_other(self, new):
        return attr.assoc(self, other=new)

    def evolve_new_other(self, new):
        return attr.evolve(self, other=new)

f = F(1, 2, 3, other=12)

for which print f.assoc_new_other(new=13) succeeds (but warns will go away) and f.evolve_new_other(new=13) fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions