Skip to content

ADR 0004: CPython-exact scalar formatting

Status: Final Type: Feature Created: 2026-07-27 Amended-By: 0025 (the Str representation this ADR first sketched now lives there); 0027 (the fixed-array literal decision moved there: a bare literal builds a list, an Array[T] annotation builds the fixed array); 0039 (the format-spec subset) Related-ADRs: 0005 (the conversion builtins this shares helpers with). Provenance: arbitrations A142, A190.

Context

The stdout differential is the project's oracle, so what a compiled program prints for a scalar must be byte-for-byte what CPython prints, floats above all, where "close" is a different program. This ADR fixes the formatting contract; the value representation it first sketched is 0025's.

Decision

Formatting is CPython's, exactly, in one place. print and str(x) share one set of runtime helpers, written once and linked by every backend, so the three backends are bit-identical on formatting by construction.

repr(float) is implemented by CPython's own definition: the shortest digit string that round-trips through strtod (tried at increasing precision), presented positionally iff the decimal exponent lies in CPython's (-4, 16] window, else scientific with a two-digit-minimum exponent; inf, nan and -0.0 spelled as CPython spells them. repr of a number or bool is str of it.

f-strings lower to a concatenation chain over their parts: Str verbatim, scalars through the same conversion helpers the casts use, so an f-string introduces no second formatting path. Format specs are 0039's subset. !s and !r lower; !a is refused, since ascii()'s escaping needs the Unicode printability tables.

Width boundaries have stated reasons: narrow ints (Int8…Int32, UInt8…UInt32) widen to Int64 and print. UInt64 refuses (values above 2⁶³ would misprint through a signed format). Float32 refuses (CPython reprs the double a float32 widens to, and matching that is a separate decision no one has demanded).

A value prints as the type it has, so a slot never changes a value's type. CPython keeps an int an int where float is declared: a widened slot would print 1.0 where CPython prints 1, and round past 2**53. An int is therefore never held by a float or complex slot, declared or not, a literal included: an assignment, an argument, a return, a field and an element store all refuse it (PP410, A190), and the message says to write 0.0 or float(n). An int literal beside a float in arithmetic or a comparison (x * 2, x < 0) is the float it denotes, since CPython computes with that float too. The same rule holds one kind down: CPython keeps True a bool where int is declared, so a bool is not held by an int slot at an assignment, an argument, a return or an element store; int(b) says it. Arithmetic is not a store, so n + True is an int, as in CPython. The rule is the frontend's and not the IR verifier's, because at the IR level a bool used as an int index is a representation question and has an answer.

Consequences

  • Every scalar a POST program can print prints CPython's bytes, on every backend, and the differential re-proves it on every build.
  • One formatting authority means a formatting change is one edit and cannot introduce backend skew.
  • Each width boundary states its reason, so it can be re-checked when a program demands the construct.
  • A program that stores an int literal where a float is declared (total: float = 0) is refused until it writes 0.0; about seven corpus programs do (A190).

Alternatives considered

  • printf-style formatting with a tolerance in the oracle: rejected outright: a tolerant oracle stops being an oracle, and float text that is "close" is a silently different program.
  • Per-backend formatting: rejected; three implementations of shortest-round-trip is three chances to disagree in the last digit, which is exactly the class of skew the shared-runtime pattern exists to prevent.
  • A declared float slot converting an int (A182, A185): it printed 0.0 where CPython prints 0, a divergence the declaration did not justify; A190 withdrew it.