3.0 KiB
Attrs source notes
Repo: python-attrs/attrs
Local checkout: /home/ubuntu/repos/rodin-sources/attrs
Why this repo is useful
attrsis a strong source for explicit data-carrier design: generated methods, constructor-shape control, and deliberate conversion at boundaries.- Its docs are especially valuable because they include caveats and failure cases, not just happy-path examples.
Declarative fields are the default for data-heavy classes
Repeated evidence
docs/examples.md:24-44shows@definewith typed fields immediately generating constructor, repr, and equality behavior.docs/examples.md:31-44makes the generated behavior visible at the REPL rather than implicit.docs/examples.md:51-58shows the same declarative shape without relying on type annotations, viafield().
Why it matters
Repeated signal: when a class mostly carries data, attrs prefers declaring fields and letting the library generate the boilerplate. The code emphasizes structure and invariants over handwritten dunder noise.
Caveat / counterexample
docs/examples.md:60-62 warns that mixing field() declarations without annotations flips attrs into a no-typing mode and can ignore annotation-only attributes. That is a sharp edge worth preserving in synthesis: declarative does not mean "mix styles freely."
Keyword-only fields are used to protect call-site clarity and inheritance
Repeated evidence
docs/examples.md:147-157showsfield(kw_only=True)forcing explicit construction at the call site.docs/examples.md:159-172shows decorator-level@define(kw_only=True)applying the same rule to the whole class.docs/examples.md:176-191shows the practical inheritance payoff: subclasses can add required fields even when the base class already has defaults.docs/examples.md:193-205shows the counterexample whenkw_only=Trueis omitted: invalid attribute ordering raises aValueError.
Why it matters
Repeated signal: keyword-only fields are not just cosmetic. They are a tool for making constructor calls self-describing and for avoiding inheritance-order traps.
Serialization is explicit and filterable
Repeated evidence
docs/examples.md:211-217showsasdict(...)as a deliberate conversion step from object to plain data.docs/examples.md:219-235showsasdict(..., filter=...)excluding sensitive fields like passwords.docs/examples.md:238-253shows built-in include/exclude helpers for more reusable serialization control.
Why it matters
Repeated signal: even value-like objects are not assumed to be wire-ready. attrs makes the serialization boundary explicit and gives callers hooks to shape what crosses it.
Pattern candidates supported by this repo
- use declarative field definitions for data-carrier classes
- prefer keyword-only construction when call-site clarity or inheritance safety matters
- keep serialization as an explicit step
- preserve caveats about mixed declaration styles and constructor ordering