Skip to content

ADR 0042: read-only class attributes, and isinstance as the constant it always was

Status: Final Type: Feature Created: 2026-08-04 Amends: 0023, extending the class surface with class-level bindings and isinstance Amended-By: 0065 (A161: on a value of several classes, isinstance tests the instance's class when they answer differently) Related-ADRs: 0032 (monomorphisation and the MRO), 0033 (the priced alternative for field shadowing), 0038 (how a union payload narrows instead). Provenance: arbitrations A125–A126.

Context

Two class-surface questions land together because both admit exact compile-time answers for the same underlying reason: no compiled value holds class-level state at runtime. A value typed by one class holds an instance of exactly that class. A class-level binding read through an instance, and isinstance, are both constants, provided the boundaries below hold.

Decision

Class attributes are read-only; a read is a constant

class Config: default_value = 100 records (dtype, literal) on ClassInfo; a read (Config.default_value, self.default_value, super().x) emits the literal at the read site, resolved through the MRO at compile time as an inherited method is: the first class binding the name answers, which is CPython's lookup order, so a subclass's default_value = 200 wins. No storage exists anywhere, because a class attribute is read-only in this subset. One binding shared by all instances would be mutable state across instances (and, compiled, across tasks) that this memory model has no slot for. Each store spelling (c.attr = v, Config.attr = v, Config.attr += 1) is refused with that reason.

Three boundary rules follow, each CPython's.

  • A bare class-level annotation binds nothing. n: int at class level creates no attribute in CPython; it is a no-op here, and P.n meets the has-no-class-attribute refusal both places.
  • A non-literal value is refused: a class-level [] is CPython's shared-mutable-default footgun. A non-literal is evaluated at class-body time, which this compiler does not model.
  • A field shadowing a class attribute is refused, because of the window CPython opens: before self.n = 7 runs in __init__, CPython reads the class value through self, and a zeroed slot would read 0, a silent divergence. The faithful alternative (ADR 0033's definite-assignment pass over self.* inside __init__) is priced and waits for a program that needs the idiom.

isinstance: a constant where the type decides it

isinstance(x, T), with T a class in the module, a builtin type name, or a tuple of either, emits the Bool constant CPython would compute when x's type decides it. An instance of one class walks the MRO statically. Scalars and containers map by dtype, with bool a subclass of int and an Array[T] answering as the list it is interpreted. A tuple ORs its members. The constant is exact: a value typed by one class holds that class, and no value is of type Any. A value that may be of several classes (0065) gets a constant when its classes all answer alike, and otherwise a test of the instance's class number.

The cases the type cannot decide are refused by name: a union payload, a CPython handle, and a caught exception. A union payload answers with its runtime tag (narrow with match or is None instead, 0038). A caught exception's class is the question the program is asking, and only CPython knows it.

callable, issubclass, type(x) and tuple literals, for the same reason

callable(x) is a constant. A name is a function, a class or a builtin, or it is a POST value. The one callable POST value is a function value (0070), since __call__ is not lowered. issubclass between two of this module's classes, or with object, is the static MRO. type(x)'s text (<class 'int'>, or <class '__main__.Dog'> for a class, with the module named as CPython names it), its __name__, and type(x) is C / == C are compile-time answers, with the argument evaluated. A tuple literal's elements are evaluated in order, and then len, a literal index, and isinstance(..., tuple) on it are known. A CPython handle or a union is refused for each of these, because its answer is decided at run time. type(x) of a value of several classes is refused too, and so is type(x) as a value anywhere else, since a class object is not a value this compiler has.

Consequences

  • isinstance, the feature long expected to force a type tag, did not: the static answer is the dynamic answer for a value of one class. The class number an instance holds came with values of several classes (0065), for method dispatch first.
  • Neither feature reaches a backend where the type decides it: a class-attribute read is a literal and isinstance is a Bool constant before any backend sees the IR.