attr

A tiny Python decorator that sets attributes on a target function or class in a DRY way.

Library
PyPI
v0.3.2
10stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
22/100Needs Attention
Development Activity0
Maintenance0
Community16
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture68
Code Quality70
Innovation58
Learning Curve95

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_attr alias 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_description and admin_order_field on 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search