ADR 0040: __repr__, and every position that dispatches to it¶
Status: Final
Type: Feature
Created: 2026-08-04
Amends: 0023, lifting the dunder gate for __repr__
Amended-By: 0063 (__str__, which str positions now call before __repr__)
Related-ADRs: 0026 (the ownership discipline the result joins), 0028 (repr on the ASCII domain), 0032 (monomorphised methods), 0039 (format-spec strictness). Provenance: arbitrations A121–A122.
Context¶
ADR 0023's dunder gate refused each special method but __init__. Its stated reason is the contract here: lowering a protocol method without lowering the dispatch that calls it would accept the definition and silently never call it, the accept-then-wrong shape. The rule is therefore general: a protocol method lands only together with all the positions that dispatch to it, or not at all.
Decision¶
Every position, at once¶
| position | lowering |
|---|---|
print(x) |
__str__ if the class has one, else __repr__ (0063), then __pp_print_str |
str(x) |
the same: CPython's object.__str__ falls back to __repr__ |
repr(x) |
__repr__ |
f"{x}" / f"{x!s}" |
through _str_of, same as str(x) |
f"{x!r}" |
through _repr_of |
print(xs) for xs: list[C] |
__repr__ per element, on the list's borrowed pointer |
x.__repr__() |
an ordinary method call, which it always was |
A class without __repr__ shows the default repr (A179): CPython's form, <__main__.C object at 0x…> (the module's name when not a script), with the instance's address in lowercase hexadecimal from a one-line helper, __pp_obj_addr. An address is unspecified, hence this runtime's own: one object shows one address and two live objects two, which is what Python specifies. Every position above shows it, and so do a list or dict printed and the message of a KeyError on a missing instance key.
__repr__ itself is monomorphised, inherited, and called as any method (ADR 0032). Its one special feature is the gate: the definition must be exactly def __repr__(self) -> str. CPython calls the protocol with no arguments and requires a str result, so any other shape defines a method the dispatch could never call. The definition is refused, which lets each call site skip arity and return-type ceremony. Shapes the general annotation rules already refuse (*args, a missing return annotation) stay refused by those rules first.
Ownership adds nothing new to the model. The receiver of a __repr__ call is borrowed (for a list element it is the list's own pointer, which a method call only reads). The returned Str is owned at the call site, released by the same site discipline as any other producer's result (ADR 0026).
What stays refused, each with a CPython reason¶
- A format spec on an instance (
f"{x:>10}"):object.__format__raisesTypeErrorfor any non-empty spec, so this is an error interpreted and must stay one compiled. A padded__repr__would print where the interpreter raises, the silent-divergence class ADR 0039's strictness exists for. !rcombined with a spec (f"{x!r:>10}"): CPython converts first and pads the result; only the plain form of each half is wired.!a:ascii()'s escaping needs the full Unicode tables, which ADR 0028's ASCII-domain answer does not cover.repr(e)on a caught exception: CPython's text isValueError('boom'), so the builtin keeps that refusal and does not fall through to the dispatch.- A dataclass whose generated
__repr__is not lowered, since its fields cannot all be formatted: the default repr would print where CPython prints the fields.
Consequences¶
- Seven positions changed at once and cannot drift apart: each reaches the same monomorphised method under the same exact-signature gate.
repr(s)onStrlowers through 0028's ASCII-domain helper.