ADR 0032: Single inheritance, by monomorphising the receiver¶
Status: Final Type: Feature Created: 2026-07-31 Amends: 0023 (lifts its refusal of inheritance and extends the layout and field rules) Amended-By: 0065 (A161: a base-typed value may hold a subclass and dispatches on the instance's class) Related-ADRs: 0024 (the memory model), 0035 (the slot descriptor, computed over an inherited layout), 0040/0042 (later rungs extending this one). Provenance: pygo upstream question U49.
Context¶
Spec §5.1 permits single inheritance; §5.2 refuses multiple (PP010, in the checker since before this ADR). 0023 refused single inheritance outright. The spec leaves the meaning of inheritance open: whether a subclass instance may be passed where the base is declared, and whether an override dispatches dynamically when it can. This ADR decides both, in the direction the rest of the compiler already leans.
Decision¶
A receiver's static type is its class¶
Inside a method copy, the receiver is an instance of exactly one class, the class the copy was made for (below). That makes a call on self a compile-time question, answered without a vtable or an indirect call. A value typed by a base class, or by a union of classes, may hold an instance of a subclass. A method called through such a value dispatches on the instance's class (0065), and the copy that call reaches knows its receiver's class exactly.
Layout: the base's fields first¶
A subclass's field layout is the base's fields, in the base's order, followed by the ones the subclass's methods bind (0023's field rule). Seeding the mapping has three consequences. A base's field keeps its slot index in every subclass, so the base's methods write the slots they were written to write. A field read through a base-typed value is one read whatever the class. self.value = ... in a subclass whose base already declared value is a store and makes no second slot, which is CPython's rule. The slot descriptor (0035) reads the assembled field list, so it covers the whole layout unchanged.
A subclass whose base has an __init__ must call super().__init__(...) as a top-level statement. This is the one new refusal that concerns correctness. CPython leaves the base's attributes unset when a subclass forgets, so the first read of one raises AttributeError. Compiled, the slot exists and holds the zero calloc left, and all three backends print 0 and exit 0 where the interpreter raises and exits 1. No instrument here sees that divergence: no memory leaks, none is freed early, the emitted code is well-formed. Only the CPython differential sees it, and only when the field is read. A field that an under-initialised subclass never reads is a divergence that leaves no trace, so the frontend refuses it; a test would miss it. The call must be top-level for the layout rule's reason: a conditional call yields a layout that only one branch initialises.
Dispatch: one copy of an inherited method per receiver¶
An inherited method is lifted once per class that inherits it, with self typed as that class. Shape.describe seen from a Rectangle is a different method from the one seen from a Shape. Inside it, self.area() resolves statically, by name, against Rectangle's table, to Rectangle.area. Inside a copy this needs no vtable, type tag or indirect call, and has no runtime cost. It produces the answer dynamic dispatch would produce, because the copy's receiver is an instance of that one class.
The copies form a fixpoint closure: seed with each method that a class exposes, then add whatever each copied body calls through super(), until nothing changes. The super() scan is syntactic and over-approximate: an unnecessary copy is dead code, a missing one is an undefined symbol at link time. The cost is code size, bounded by classes × depth × methods and independent of a program's input.
super() is a spelling of a call, not a value¶
super().m(...) and super(Class, self).m(...) are resolved at compile time: super() names the method the defining class inherits. The body it appears in is already a copy monomorphised for one receiver, so both ends of the edge are known. No proxy object is built, so super() in any other position is refused with a message about super():
- bound to a name, returned, passed: PP900, "a spelling of a call, not a value". Before that message existed, the construct reached the ordinary call machinery and came back as PP502 "call to unknown function
super", which is wrong twice:superis known, but the method call around it is missing. super().x, a field: PP900 (a class attribute read this way is 0042's constant). A subclass's layout contains its base's, sosuper().xandself.xname the same slot and aliasing them would compile. CPython'ssuper()searches the class, sosuper().xfinds a class-level attribute and raisesAttributeErrorfor an instance one ; that is the opposite of what the aliasing gives. The message says to writeself.x.
The key is a pair (receiver, defining class). Neither alone identifies a copy. GrandChild.get calls super().get(), which is Child.get. That calls super().get() for Base.get. All three copies take a GrandChild receiver. Resolving super() against the receiver's base, when it must be the defining class's base, turns the middle link into a call to itself.
The explicit form is lowered only as super(ThisClass, self): the pair naming the method's class and receiver. Any other pair selects a different part of the MRO, so resolution would depend on a value and lose the anchor of the enclosing class; it is refused. A restriction naming a fact the compiler independently knows is a checkable redundancy, worth more than the smaller language it costs. Base.method(self, ...) inside a method of a class Base is an ancestor of, or is, calls Base's own method, as super() does (0065).
object as a base¶
class C(object): is class C:. In Python 3 every class derives from object, so naming it changes nothing, and code written to run on both Pythons names it on every class. A class of the module named object is that class, per Python scoping. super().__init__() with no arguments, in a class whose base is object, is object.__init__(), which does nothing, and lowers to nothing in both spellings, since code written for cooperative inheritance calls it from every __init__. Any other method reached through super() there, or __init__ with arguments (a TypeError in CPython), is refused.
Alternatives considered¶
One copy per method, with a subclass pointer widened to its base at the call site. The layout is prefix-compatible, so a Rectangle * is a valid Shape *; the design costs one method and needs no closure. And it is silently wrong: Shape.describe calling self.area() then reaches Shape.area, printing rect with area 0 where CPython prints rect with area 12, on all three backends identically, so nothing in the cross-backend apparatus can see it. Refusing that shape instead would need a whole-program analysis ("is any method this body calls on self overridden anywhere?"); monomorphising it needs none. The program that exhibits the failure is in the integration suite as the thing that would have caught the design.
Consequences¶
- The memory model needed one new program and no new rule. An inherited
Strfield is the same descriptor position reached a second way: the descriptor is computed over a layout assembled from two class bodies. - Filed upstream (U49): does POST inheritance include subtyping? The spec permits single inheritance and is silent on both questions: whether a subclass instance may sit in a base-typed slot, and whether an override dispatches dynamically when it can. 0065 answers yes to both, with a class number in each instance. Two implementations could satisfy §5.1 and disagree about what
describe()prints. - 0023's refusal boundary states what the class surface refuses and why;
is,id()and__del__stay refused for its reasons.