Skip to content

ADR 0039: The format mini-language subset

Status: Final Type: Feature Created: 2026-08-03 Amends: 0004 (f-strings gain specs) Related-ADRs: 0025 (the ncp field widths count on), 0028 (which took this ADR's generator-expression half in the 2026-08-07 consolidation)

Context

CPython's format mini-language is large (fill, align, sign, #, 0, width, grouping, precision, a dozen types), and a spec that is ignored produces different bytes on stdout, the defect class the differential exists to catch. A formatting implementation that is 95% right prints the wrong text 5% of the time and gives no signal.

Decision

The accepted grammar is exactly [[fill]align][0][width][,][.precision][type], and everything else is refused with a diagnostic naming the spec. Strictness is the decision. Each row lands with a differential row:

  • an ASCII fill character before the alignment (:.<8); alignment < > ^;
  • 0-fill without an alignment, sign-aware for a number (-0042) and the fill for a string;
  • a literal width or one replacement field;
  • , grouping on an integer or a float;
  • .N with a float type; f, e, E, g and G default to six digits, as CPython's do;
  • types s; d, x, X, o and b on an int, the digits without hex/oct/bin's prefix and the sign before them (format(-255, "x") is -ff), a bool formatting as its int; f, e, E, g and G on a number, through libc's %.*f, %.*e and %.*g, since CPython's float.__format__ spells these types as C does, with a NaN written nan or NAN whatever its sign bit, where printf may write -nan. A float under an integer type is refused, where CPython raises ValueError.

printf-style % formatting with a literal format string, or one a module constant names, is rewritten to the equivalent f-string and lowered as one, so the bytes are this ADR's. The conversions:

  • %s and %r are the !s and !r conversions, right-aligned under a width, as printf aligns.
  • %d, %i and %u are type d, a float argument wrapped in int(), since CPython's %d truncates.
  • %x, %X, %o, %f, %e, %E, %g and %G are their types, with a width, a precision and the 0 and - flags.
  • %c of an int is chr(n), and %% is a percent sign.

Each argument is lowered once into a hidden local before any is formatted, which is CPython's order, and the argument's type picks the spelling. A tuple on the right supplies the elements, and so does a name bound to a tuple. A format a module constant names (fmt % i, where the top level binds fmt once to a str constant) formats with that text. That holds in a function where no local shadows fmt, and at the top level after the binding. A module constant folds str(c) of a folded int, bool, float or str with CPython's own str, so fmt = '%0' + str(DIGITS) + 'd' is '%04d'. A name bound twice stays a run-time format and is refused, since the constant table keeps the last binding, which an earlier read would not see. A mismatched argument count and %d of a str are refused: they are CPython's two TypeErrors.

A spec lowers in two independent steps: the type step turns the value into a Str, the width step pads it. Neither knows about the other, so a dynamic width (f"{s:<{w}}") costs nothing extra: it is an ordinary integer expression handed to the pad call.

Two properties come from CPython itself, because the compiler calls the same mechanism:

  • %.*f under the default rounding mode is the libc path CPython's float.__format__ takes, so format(2.675, '.2f') is 2.67 in both: the double nearest 2.675 lies below it, and both round half-to-even off the same bits. A differential row keeps the agreement structural.
  • Width is counted in code points, off the Str header's ncp: a two-code-point, three-byte string padded to width 3 gains one unit. A byte-counting implementation would be wrong on every non-ASCII string.

Default alignment is per type (numbers right, everything else left), resolved in the frontend, because only the frontend knows the static type.

Two parser rules look like bugs and are spec. A leading zero (f"{n:03}") must not parse as a plain width: "03".isdigit() is true, and dropping the zero prints 7 where CPython prints 007. Alignment matching must test membership in a set of characters, never in "<>^". A test against a string differs from one against a set on the empty spec, which a dynamic width always produces.

Consequences

  • A spec the compiler accepts formats byte-for-byte as CPython. A spec it does not accept gets a compile-time diagnostic.
  • The subset grows by grammar rows, each landing with a differential row; because the type and width steps are independent, a new type or alignment is one helper.

Alternatives considered

  • Implementing the full mini-language at once: rejected. The corpus demanded four forms; any unexercised form would be an untested claim about output bytes.
  • Ignoring unknown specs (format as str(x)): rejected without discussion, as a silent divergence.