Skip to content

ADR 0060: Module-level values, owned by the module

Status: Final Type: Feature Created: 2026-10-11 Related-ADRs: 0003 (a script's top level lowered as __post_main__), 0024 (the memory model this adds an owner to), 0031 (the mutable-default refusal, A88, which this reverses), 0033 (the checked read this reuses). Provenance: A144 (what a typed program is), A148, A160, the Shed Skin census (notes/measurements.md, 2026-10-11).

Context

A script's top level runs as __post_main__ (0003), so a name it binds is a local of that function. A module-level name bound to a literal is lifted out as a module constant and is readable everywhere. Any other module-level value (a list, a dict, an instance, a computed number) is invisible to the script's functions. They refuse a read of it as "not a parameter, local, module constant, or imported constant", a subscript of it as "not a lowered local array", and += on it as "augmented assignment to unknown name".

Typed Python uses module-level values from functions as a matter of course: a board, a table of moves, a cache, a counter. mypy types each from the first binding and lets functions read it; a function that rebinds one declares it global. In the census, module-level variables in functions stop 28 of 53 programs, and default arguments that are not constants stop 16, the second root this ADR answers, because a default that is not a constant is evaluated once into the same kind of storage.

Decision

A name bound by a script's top level and read by a function of the script is a module global: a static slot owned by the module, typed by the first binding in source order, readable from any function.

  • __post_main__ stores into the global where it would have stored into a local, and reads it from there.
  • A function reads it directly. A function that rebinds it declares it global, which the checker accepts in a script build and nowhere else (PP006): a library has no top level for the name to belong to. Mutation through the value (board[i] = x, moves.append(m), cache[k] = v) is not a rebinding and needs nothing.
  • A name a top-level function binds under global, where the top level never assigns it with =, is a module global too (A160), since CPython creates the name when the function first runs. It is typed by a probe lowering of the first top-level function in source order that assigns it, and always read through the bound flag, so a read before that function has run raises CPython's NameError. Other functions' bindings must agree with that type, as any field's do. The top level may update it with +=. A global that names nothing its function uses is CPython's no-op and is dropped. PP006 refuses two such names: one only a nested function or a method binds, and one the top level binds by a for or another non-assignment binding.
  • A read from a function before the top-level binding has run (the function called above the binding, or the binding inside a branch) raises CPython's NameError: name 'x' is not defined, through a per-global flag that the bindings set: 0033's checked read, at module scope. A global bound by a top-level statement that runs before any function is called has no flag.
  • Ownership: a counted global holds one reference, the module's (0024's rule with a new owner that is not a frame). The module releases the counted globals when __post_main__ returns, before the release counters report, so they still show zero; a global never bound releases its null.
  • A default that is not a compile-time constant is evaluated once, when its def runs at the top level, into a hidden module global, which a call that omits the argument uses: CPython's semantics, the shared mutable default included. A88 refused that shape because a fresh container per call would have printed a different answer from CPython's. Evaluating it once is CPython's answer, so the refusal is reversed.
  • A task that reads a module global holding a counted value is refused under the sendability rule (pygo notes/06-sendability.md): two tasks would share a mutable object with no owner between them. Reading a scalar global is a copy and is allowed.

Consequences

  • A function that reads a global costs a load where a local was a register, and a flagged read a test. The benchmarks use globals in their inner loops; the speed lane will say whether a flag that cannot fire (the binding ran before any call) needs to be proven away, which this ADR already does for the common case.
  • A library module (no __post_main__) has no place to run top-level code, so the module-level values stay refused until a module has an initialisation entry point. Script mode is where the census's programs are.
  • The module constant stays as it is: a literal bound once is still folded, and is not a global.

Alternatives considered

  • Pass module values to functions as hidden parameters: this needs no new storage. Each call site would thread the globals its callees read, transitively, which a callback or a task entry point cannot do.
  • Refuse, and ask for the value to be passed explicitly: the behaviour before this ADR. mypy accepts these programs without an annotation, so under A144 the refusal is distance.
  • Requiring every global to be bound above every def, in place of the checked read: CPython runs a program whose functions are defined above the globals they read, which is the usual layout.

Detailed design

  • The module instance. The globals are the fields of one hidden instance, of a class named __main__ (CPython's name for the module a script runs as, which no program binds). __post_main__ allocates it at entry, owns it as any frame owns an instance, and stores a non-owning pointer in the runtime (__pp_module_set); any other function that uses a global borrows it in the entry block (__pp_module_get). The instance is released when __post_main__ returns, releasing what the fields hold, so the counters end at zero. The pointer is process-wide, the one new piece of such state, and is never set in a library or an extension module.
  • A rewrite. Each use of a global, in a function where symtable resolves the name as global and in the top level, becomes __main__.name, so subscripts, method calls, +=, member types, union fields and the release walk are the field lowerings unchanged. a, b = ... with a global among the targets unpacks into locals and stores them after, left to right. A global called as a function holds a function value, which no field holds, and keeps the call's diagnostic.
  • Types from the top level as written. The top level is lowered once before the rewrite, as it always was. Each global's field takes the type and member types of the matching local there, including x = None completed by a later assignment (A147). The probe's IR is discarded. A global whose value has no field type (a tuple, an array) is refused at its binding.
  • The flag. A global needs one when some top-level statement before its first unconditional binding names a function or class of the script, or when a top-level read of it is not proven bound. The flag is a hidden Int64 field beside it, set on store; a read tests it and raises NameError with the name.
  • Defaults. A default that does not fold is assigned, at the def's place in the top level, to a hidden global __pp_default_<function>_<param> (<Class>_<method> for a method), with the parameter's annotation; a call that omits the argument uses it. A flagged one names the function (or the class) in the NameError, which is what CPython says of a call made before the def ran. A library build keeps the refusal, with a message saying a script build evaluates it.
  • A name bound once to a literal stays a module constant, folded at every read. A constant the top level binds twice, or a function rebinds, is a global: before this a function read the last literal whatever had run.
  • Tasks. Starting a task in a program with a counted module global (a str, container, instance or union) is refused at the spawn. Rebinding a scalar global from a task is not refused; it is a race CPython's lock hides.
  • Tests: tests/b_integration/test_module_globals.py (programs differential on C, QBE and LLVM with all counters at zero; the flag, the global refusals, the task refusal).