Skip to content

ADR 0065: Values of several classes, dispatched on the instance's class

Status: Final Type: Feature Created: 2026-10-26 Amends: 0024 and 0035 (the object header gains the instance's class), 0029 (a union may name several classes), 0032 (a subclass instance may be held in a base-typed slot), 0042 (isinstance is a test of the class when the answer depends on it) Related-ADRs: 0040 (__repr__ and __str__), 0038 (a narrowed optional). Provenance: A161, the Shed Skin census (yopyra, kanoodle).

Context

ADR 0032 lowered single inheritance by monomorphising the receiver, and bought it with one rule: a slot typed by a class holds exactly that class, so a value's static type is its dynamic type and every method call is a direct call. Python programs do not keep that rule. yopyra keeps the scene in a list[esfera | plano], both subclasses of cuerpo, and calls intersecta and getNormal, which only the two subclasses define; kanoodle builds [A(), B(), …, L()], twelve subclasses of Omino, and reads tile.name and calls tile.translate(...) on each. The decision taken (A161) is that a base-typed value may hold a subclass instance and dispatches on the instance's class; the two census programs need the same thing for a union of classes, so both are built on one mechanism.

Decision

  1. An instance records its class. The object header gains a word, the class's number, written by __pp_obj_new and copied by copy.copy and copy.deepcopy. The number is derived from the module and class names, so the modules of a program agree on it. A tuple, a file and the module instance hold zero; nothing dispatches on them.
  2. A value may be one of several classes. A class set is an instance type naming the classes a value may be. A class the module subclasses means, in an annotation, itself or any subclass; P | Q means either, with their subclasses; P | Q | None is a union box whose one instance case is the set. A list literal of instances of several classes has the set for element type. A slot holds a value whose classes are among its own: an instance in a slot of its base, a set in a wider set. Nothing about the pointer changes.
  3. A method called through a class set dispatches on the class. One arm per class, each a direct call to that class's copy of the method (0032's "Dispatch"), under a test of the instance's number; the arguments are lowered in each arm against that class's own signature, as CPython binds them to the method it finds. The arms' results join when they agree: one type, or classes, which join as a set. repr, str, print and f-strings dispatch the same way. A call whose receiver is one class (self, a fresh construction) stays a direct call.
  4. A field read through a class set is a direct read when the classes all keep the field in one slot with one type and one member type, which a base's field always is (0032's "Layout") and fields the subclasses bind in one order are. Otherwise it is refused.
  5. isinstance(x, C) on a class set is a constant when the classes it may be all answer alike. When they disagree, it tests the instance's number against the classes that answer yes. type(x) of a class set stays refused.
  6. A base the script never constructs is never the class of a value: a value typed by it is one of the subclasses. CPython never runs the base's methods on an instance of exactly that class, so they may read fields and call methods only the subclasses define, which is how kanoodle's Omino, amaze's MazeGame and richards' Task are written. The base's own copies of those methods are lowered, and dropped with their diagnostics when one does not lower, since nothing can call them; a field that one of those methods binds is typed through a subclass that inherits the method. A base whose line has a @classmethod counts as constructed, since cls(...) may build it. A library is never assumed to be unconstructed, since an importer may construct it.
  7. Base.method(self, ...) inside a method of a class Base is an ancestor of, or is, calls Base's own method, as super() does (0032).

These are refused by name: results of different types across the classes, container results whose member types differ, a field the classes keep in different slots or with different types, type(x) of a class set, and a set holding a class of another module.

Consequences

  • Programs without a subclass or a union of classes compile to the same IR as before, apart from the class number passed to __pp_obj_new. The instance header grows from 16 to 24 bytes.
  • The Shed Skin census fell from 935 refusals to 835: richards 84 → 42, pygmy 70 → 48, kanoodle 27 → 1, bh 29 → 26, yopyra 14 → 11, amaze and path_tracing two each. No program compiles from this alone: kanoodle's last refusal is column = root.right, a Column | None field assigned to a local typed Column, which mypy, ty, pyrefly and zuban accept: they keep the field narrowed by root.right = root across the calls and stores that follow, where 0038 ends a field's narrowing at the first of them; yopyra stops at int and float mixes and augmented assignments; richards' observed annotations name fewer classes than the methods return.
  • Each dispatched call has one copy of the argument lowering per class, so code grows with the number of classes a value may be.
  • notes/accept-then-wrong.md has no section for this. The build did meet the hazard ADR 0032's test warned of: isinstance on a class set first answered from the static type, a constant False. It is a test of the class now, which the integration test checks.

Alternatives considered

  • A vtable per class, an indirect call through the header. It needs a function-pointer IR value on three backends, and gains nothing over a comparison chain at the sizes the census has (twelve classes at most).
  • A union box per class set, the tag naming the class. Every value would be boxed twice, with the box adding nothing the header word does not hold.
  • Keeping lists homogeneous by exact class (the alternative A161 rejected).