attr
A tiny Python decorator that sets attributes on a target function or class in a DRY way.
Repository Health
Technical Analysis
attr is a minimal Python utility that provides a single decorator for assigning attributes to a function or class without repeating the target’s name on every line. Instead of writing my_func.short_description = 'Field' and my_func.admin_order_field = 'real_field' after a definition, you decorate the function once with @attr(short_description='Field', admin_order_field='real_field').
The pattern is especially handy for frameworks like Django, where calculated admin fields are configured by setting attributes on functions. Because the package name attr is shadowed by the popular attrs project, it also ships a dry_attr alias so the decorator can be imported unambiguously.
What You Get
- A single
attr(**kwargs)decorator that sets each keyword as an attribute on the target - A
dry_attralias to avoid the name clash with the attrs package - Support for decorating both functions and classes
- A dependency-free, single-file implementation
Common Use Cases
- Setting Django admin metadata like
short_descriptionandadmin_order_fieldon calculated fields - Attaching metadata attributes to functions without repeating the function name
- Configuring class-level attributes declaratively at definition time
- Reducing boilerplate anywhere a framework reads attributes off callables
Under The Hood
Architecture - The entire implementation is one function in dry_attr.py (re-exported by attr.py): attr(**names_values) returns a set_target closure that iterates the supplied keyword arguments, calls setattr on the decorated target for each, and returns the target unchanged so it can be used as a decorator. A module-level dry_attr = attr alias exists solely to sidestep the naming collision with the attrs package.
Tech Stack - Pure Python with no runtime dependencies, packaged with a classic setup.py/setup.cfg and a tox.ini for multi-version testing. It targets a wide range of Python versions given its tiny surface area.
Code Quality - The module is trivially small and includes an inline test() function demonstrating and checking the decorator’s behavior. There is little to maintain, and the project has been effectively stable and inactive since its last release, reflecting its complete-and-done nature rather than neglect of a large codebase.
API Design - The API is about as simple as a decorator can be: call attr() with the attributes you want set as keyword arguments and apply it above a definition. There is essentially no learning curve, and the only wrinkle — the attr vs attrs name clash — is documented and solved with the dry_attr alias.