Skip to content

feat: class keywords and metaclasses - #275

Merged
jonruttan merged 1 commit into
mainfrom
feat/metaclasses
Nov 28, 2026
Merged

jonruttan merged 1 commit into
mainfrom
feat/metaclasses

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

The first of the runtime PRs enum needs: classes can now have metaclasses.

What's new

  • Class keywords. A class header takes name=value and **mapping after its bases. metaclass= is taken out; the rest go to the metaclass and to __init_subclass__.
  • Choosing the metaclass. An explicit metaclass= still gives way to a base's metaclass that derives from it; otherwise the most derived of the bases' metaclasses wins. Two that don't derive from each other raise CPython's "metaclass conflict" TypeError. A plain function as metaclass= is just called.
  • type's own methods: __new__ (with its argument checks and messages), __init__, __call__, __setattr__, __delattr__ and __prepare__, so super().__new__(mcls, ...) and super().__call__(...) work from a metaclass.
  • __prepare__. The namespace it returns gets the body's names stored into it in order, so a dict subclass that watches its stores sees each one. Enum's member bookkeeping relies on this.
  • __init_subclass__. It runs when a subclass is made, by a class statement or by type(...), and takes the header's keywords. object.__init_subclass__ refuses keywords and positionals with CPython's messages.
  • Class-level behaviour from the metaclass. Calling a class, len, indexing, iteration, in, reversed, truthiness, repr, setattr and delattr on a class all ask its metaclass first. So do the metaclass's methods, properties, classmethods and staticmethods. type(C) and C.__class__ answer the metaclass.

Other changes

  • The class form the parser emits carries the keywords, so the golden tree in D0-golden changes.
  • New spec C2-metaclasses with six cases, outputs from CPython 3.14.

Known differences and what's left

  • class D(5) raises x-python's "base is not a class" TypeError. CPython calls int as the metaclass and reports "int() takes at most 2 arguments (3 given)". The exception type matches.
  • Left for the next runtime PR: arithmetic on int subclasses and __get__ on class access.
  • Not done yet: __qualname__, __mro__, and the __module__/__qualname__ entries CPython puts in the namespace.

Testing

  • Suite: 1296 tests. The one failure was a crash on class D(5), now fixed; 62-class-surface and C2 re-run clean.
  • Conformance: 644 tests with 9 failures, all from the known set.
  • if-ladder check is clean.

🤖 Generated with Claude Code

A class header now takes keywords and **mappings after its bases.
metaclass= picks the class's metaclass; otherwise the most derived
of its bases' metaclasses is used, and two that do not derive from
each other raise "metaclass conflict". The rest of the keywords go
to the metaclass and to __init_subclass__.

type gains __new__, __init__, __call__, __setattr__, __delattr__ and
__prepare__. A metaclass's __prepare__ makes the namespace the body's
names are stored into, in order. object.__init_subclass__ refuses
keywords, as CPython's does, and a class's hook runs when a subclass
is made, by a class statement or by type(...).

A metaclass's methods, properties, classmethods and dunders now reach
its classes: calling, len, indexing, iteration, in, reversed, bool,
repr, setattr and delattr on a class all ask the metaclass first.
type(C) and C.__class__ answer the metaclass.

The class form the parser emits changes to carry the keywords, so the
golden tree in D0 is updated. C2-metaclasses covers it, with outputs
from CPython 3.14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit b5d6e3a into main Nov 28, 2026
12 checks passed
@jonruttan
jonruttan deleted the feat/metaclasses branch November 28, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant