Repository navigation
Description
I am researching how we can expand the field with more metadata on our project. The documentation was very helpful for getting started, but I've hit a few places where there is a bit of friction
Adding field decorators requires extending attr._make._CountingAttr
This is way more complicated then it seems at first glance because:
- the
_CountingAttrkeeps an internal counter for the ordering - changing the return type of
fieldmakes it be picked up as a customdefaultinstead of an extension of_CountingAttr
The initial design I considered was
def field(*, metadata=None, **kwargs: Any) -> Any:
if not metadata:
metadata = {}
metadata.setdefault("my_ext", MyMetadata())
return MyCountingAttr(attrs.field(**kwargs, metadata=metadata))
class MyCountingAttr(attr._make._CountingAttr):
def __init__(self, ca):
for f in ca.__slots__:
setattr(self, f, getattr(ca, f))
attr._make._CountingAttr.cls_counter -= 1
self.counter -= 1
def new_decorator(self, meth):
my_metadata: TmtAttrsMetadata = self.metadata["my_ext"]
my_metadata.func = meth
return meth
@attrs.define
class MyMetadata:
func: Callable[..., Any] | None = None
@attrs.define
class Example:
x: field()
@x.new_decorator
def _x_my_func(self):
passbut this fails when it gets to
Lines 403 to 407 in f0e420b
| ca_names = { | |
| name | |
| for name, attr in cd.items() | |
| if attr.__class__ is _CountingAttr | |
| } |
Could not find a way around it besides changing that to a
issubclass check
Many useful internal functions are not exposed
For example attr._make._determine_whether_to_implement would be really nice to not have to re-implement, especially when it comes to the correct handling of classmethod. Admittedly my usage there is quite weird (injecting some methods/classmethods based on inputs to the @define decorator) and it could be better handled with inheritance/metaclass, but it's the first example of the internal helpers that I've found in my code.
Activity
LecrisUT commentedon Apr 13, 2026
Quite related, afaiu
_CountingAttris the reason we must usefieldto allow for decorator approaches. But withAnnotatedalso becoming a thing, would there be a way to move this logic. I don't know if it's a limitation in the python parser that would not be able to resolve where to get the decorator from. Probably there is already research on this in an issue?hynek commentedon Apr 16, 2026
I don't think we can ever conjure a decorator into class scope. The closest thing I can think of is something like
@attrs.converter("field_name", …)which I think is what Pydantic does. However, that's not really type-checkable (bummer we can't goattrs.fields(C).x) and unless you wrap it kinda against your ultimate goal of wanting to expand it?I honestly also don't think that the need to use
field()is that bad…as a side note: I wanted to remove the counting for like 5 years, but it would definitely break some older software. I'm not sure what to do about it except collecting twice and warn if the result differs? Sounds like a nontrivial performance regression tho. :(