-
-
Notifications
You must be signed in to change notification settings - Fork 408
Closed
Description
I've run into many cases where, to avoid duplicating types and defaults, it would be nice to be able to "transfer" attributes between classes.
Example: We would like to do the following:
@attrs
class InnerClass:
a = attrib(type=float, default=3.)
@attrs
class OuterClass:
inner = attrib(type=InnerClass)
b = attrib(type=int, default=1)
@attrs
class BuilderClass:
a = InnerClass.a # <- This Line fails, forcing us to duplicate types and defaults
b = OuterClass.b # <- This one too..
def build_inner(self) -> InnerClass:
return InnerClass(self.a)
def build_outer(self) -> OuterClass:
# Just to demonstrate why we use this builder class
return OuterClass(inner=self.build_inner(), b=self.b)
builder = BuilderClass()
inner = builder.build_inner()
assert inner.a == 3.
builder = BuilderClass(a=4.)
inner = builder.build_inner()
assert inner.a == 4.
It would be really nice if attrs supported this syntax:
@attrs
class BuilderClass:
a = InnerClass.a # <- This Line fails
Instead, we usually end up just doing
@attrs
class BuilderClass:
a = attrs(type=float, default=3.)
which requires us to duplicate type/default/validators/doc/etc.
Is that something that Attrs would be willing to support?
jonathonsather-bc and kirillpir
Metadata
Metadata
Assignees
Labels
No labels