Skip to content

ADR 0031: The empty container, and why it is one rule for three types

Status: Final Type: Feature Created: 2026-07-31 Related-ADRs: 0027 and 0030 (the two container ADRs this rule spans), 0029 (the hint-threading arrangement the member types travel through), 0024 (class fields), 0060 (a default evaluated once), 0038 (the narrowing an inferred T | None gets). Provenance: arbitrations A86 (decided as option 4, with option 1 as the corpus half), closing A4; A2 and A13 constrain it; A144 and A147 amend it.

Context

[], {} and set() have no member type. A rule for list belongs in 0027 and a rule for dict and set in 0030, but two documents answering one question is how container types come to disagree about it. This is therefore one ADR and one rule; writing it twice is the failure it exists to prevent.

The rule answers a general question, where does a value with no type of its own get one from: the question x: Int32 = 5 answers for a bare integer and x: int | None = None answers for a union case (0029). Those already agree on an answer; this makes the container agree with them.

Decision

An empty container takes its member types from an annotation in scope at the site, or else from the first use that fills it, as a type checker infers them. With neither it is provably empty, and a use that would need a member type is refused with a message that names the annotation to write.

The spellings

[], list(), {}, dict() and set() are the empty containers. set() is the only spelling an empty set has, since {} is a dict in Python. The constructors had to be in the rule from the start. A rule built for the literals would have covered list and dict and left set with a value that could not be written, the one asymmetry this rule exists to prevent. list() and dict() come along because refusing the symmetric spellings once set() must be accepted would make one type's accident the language's shape. A constructor that consumes an iterable (set(x), dict(pairs), list(xs)) is not this rule.

The five positions that supply the annotation

  1. an annotated binding: xs: list[int] = [];
  2. a declared return: def f() -> dict[str, int]: return {};
  3. an annotated parameter, at the call: f([]) where def f(xs: list[int]);
  4. a name already bound: xs: list[int] = [1] … xs = [];
  5. a class field's annotation: self.items: list[Item] = [].

The fourth is a position without an annotation that belongs in the rule, because the binding's type is a fact. x: Int32 = 0 then x = 3 has kept x's width since before there were containers, and refusing the container version would be the same inconsistency in the other direction. The position also covers xs = [1] then xs = []: nothing in the program is annotated; the element type was decided by the first literal. The fifth has no alternative source: a field is not a local, so no binding annotation, declared return or annotated parameter can supply the field's member types.

The member types travel beside the dtype hint through one hinting function, the arrangement 0029 established for a union's case list. Threading them through that one function covered the fifth position with the same mechanism as the first four, and covers any later one without further work.

A member type from the first use, as mypy infers it (A147)

A typed program has the annotations a type checker requires and no others, and what the checker infers, postpile infers (A144). mypy infers xs = [] followed by xs.append(1) as list[int] without an annotation (its partial types), so this rule takes mypy's rule exactly. An unannotated empty container bound to a name takes member types from the first use of that name after the binding, in source order, in the same scope, when that use is one of the completing uses mypy reads:

  • a list: xs.append(v), xs.extend(ys) and xs += ys (which is extend in place), taking v or ys's element;
  • a set: s.add(v) and s.discard(v), taking v;
  • a dict: d[k] = v and d.update(e), taking k and v, or e's key and value.

A completing use inside a branch or a loop counts, as it does for mypy (the first in source order is the one). A use that mentions the name again (xs.append(len(xs)), d[k] = len(d)) completes nothing; mypy refuses those too. Anything else as the first use (a read such as len(xs) or print(xs), xs.insert(0, v), a crossing into a call) leaves the container without member types, so that a read before the completing use forfeits the inference, as it does for mypy. The alternative, a look-ahead that skips reads, would accept programs mypy refuses.

x = None is completed by its next assignment in the same scope, as mypy completes it, even when x is None is read before that assignment (the running-maximum idiom): the binding is x: T | None = None, where T is the type of the assigned value. T is a scalar (int, float, str, bool) or a class instance; any other value leaves None refused at its binding. Narrowing is 0038's.

A container with no member type is provably empty

When neither an annotation nor a completing use gives an empty container its types, it is of Nothing, a member type no value has. No value may be added to it. An append, a store, a crossing into a declared type and a concatenation with a differently typed list are refused with this ADR's message, which quotes the use when it is a mutation and says that mypy would ask for the same annotation. A use that reads no member is exact: printing, len, truthiness, sorted, slices, + and *, isinstance, x in c (False), c.count(x) (0), ",".join([]) (""). No type is inferred or observed here, because no member ever exists to observe. xs = [] followed by xs = [1] rebinds to a list of ints, as in CPython, because Nothing is never handed on as a hint.

The message looks ahead only: a mutation before the binding is not quoted. An earlier append on a same-named binding says nothing about this one, and quoting it would be the compiler inventing a connection the program does not have.

Refusals, each a decision

A parameter default: def f(xs: list[int] = []). The annotation is right there, so the rule could fill it; this is the one position where filling it would be wrong. CPython evaluates a default once, when the def runs, so every call that omits the argument shares one container: Python's mutable-default trap. A compiled POST that allocated a fresh container per call would print different numbers from the interpreted program on the accumulator idiom, a silent wrong answer where every other refusal in this compiler is a diagnostic. A script build evaluates the default once, where the def runs, into a module global (0060, A148), which is CPython's answer and reproduces the trap. A library build has no top level to run it in and refuses the default, naming the trap.

Heterogeneous member types are untouched. list[object], a bare list, a bare dict, dict[str, object], a mixed literal: all still refused, and an empty container annotated with one of them is refused by the annotation's diagnostic before this rule is consulted. This ADR lets an annotation or a completing use supply member types; what an annotation may say is unchanged (A13 stays deferred).

Consequences

  • The corpus half is observational, matching the rule. The harvest tool annotates empty-container bindings from what the golden runs put in them and declines whatever it cannot observe: a class body (an annotated assignment there writes __annotations__, which changes the program), a global/nonlocal name, a parameter (its type is the def header's business), a name the scope binds more than once, a container observed holding two kinds of thing (A13), or a container the run leaves empty. The last is never defaulted. An always-empty container would accept any member type, so annotating one looks free; it would be a guess presented as an observation, in a tool that claims to annotate only what the runs exhibited.
  • That decline is the rule's ergonomic price: a container that is never filled and never annotated stays of Nothing, which is exact for every read and refuses every fill.
  • An empty container owns only its header, with no element buffer allocated. At zero elements an element-ownership defect is absent, so every instrument is correct to stay silent. The operational rule concerns test sets: a program whose container is empty is never coverage for that container's ownership positions.
  • No new dtype family and no new runtime helper: release goes through the rows the verifier already contains.
  • A function that completes x = None is lowered twice, the second time with the binding rewritten; a program pays that only in the functions that use the idiom.

Alternatives considered

  • Flow inference refused permanently (A86 as first decided): it rested on POST's types being declared (A2), which A144 overtook, on a syntactic rule being wrong in general, which taking mypy's rule exactly answers, and on a rule per position, which does not arise, since the completing use is one more source beside the positions. The census put the annotation it asked for in front of 31 of 53 programs once their classes compiled (notes/measurements.md, 2026-10-11).
  • Per-container rules in 0027 and 0030: rejected: the measurement behind A86 is that the same one-line blocker appears in three spellings across the corpus, so it is one rule for three types or none.
  • Defaulting a member type the run could not observe: rejected in the harvest tool for the same reason the compiler does not guess: it is inference dressed as observation.