Open
Description
A user might want to use attributes from an already @define
d class to populate a new attrs class. However, _ClassBuilder
expects all given attributes to be instances of _CountingAttr
instead of Attribute
instances, which raises an exception when provided:
import attrs
@attrs.define
class Example:
a: int
ExampleCopy = attrs.make_class("ExampleCopy", attrs={"a": attrs.fields(Example).a})
# AttributeError: 'Attribute' object has no attribute '_validator'. Did you mean: 'validator'?
@attrs.define(these={"a": attrs.fields(Example).a})
class ExampleCopy:
pass
# AttributeError: 'Attribute' object has no attribute '_validator'. Did you mean: 'validator'?
I would either expect _ClassBuilder
to handle both kinds of attributes, or there be an officially sanctioned function allowing you to convert an Attribute
to a _CountingAttr
for cases like this. You can write this conversion function manually, something like:
def convert_to_countingattr(attr):
"""
Convert an `Attribute` instance to an equivalent `_CountingAttr` instance.
"""
return attrs.field(**{
slot: getattr(attr, slot)
for slot in attr.__slots__
if slot not in {"name", "eq_key", "order_key", "inherited"}
})
But this feels janky and prone to breakage.
Metadata
Metadata
Assignees
Labels
No labels