Skip to content

Support evolving to a subclass (possibly any class)  #1088

Open
@fredsensibill

Description

@fredsensibill

Recently I wrote a modification to the attrs.evolve function that works like this:

@attrs.define
class Foo:
  a: int

@attrs.define
class Bar(Foo):
  b: str

foo = Foo(1)
bar = evolve_as(foo, Bar, b="2")
print(bar)
# Bar(a=1, b='2')

This was very useful for my particular use-case, which involved enriching a data object in a pipeline, so I thought that this might be a good fit for attrs.

My implementation did not require Bar to be a subclass of Foo, but I'm not sure about the design implications of that. Might be better to limit this to subclasses only.

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