Repository navigation
feat: class keywords and metaclasses - #275
Merged
Merged
Conversation
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>
This was referenced Nov 29, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The first of the runtime PRs enum needs: classes can now have metaclasses.
What's new
name=valueand**mappingafter its bases.metaclass=is taken out; the rest go to the metaclass and to__init_subclass__.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 asmetaclass=is just called.type's own methods:__new__(with its argument checks and messages),__init__,__call__,__setattr__,__delattr__and__prepare__, sosuper().__new__(mcls, ...)andsuper().__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 bytype(...), and takes the header's keywords.object.__init_subclass__refuses keywords and positionals with CPython's messages.len, indexing, iteration,in,reversed, truthiness,repr,setattranddelattron a class all ask its metaclass first. So do the metaclass's methods, properties, classmethods and staticmethods.type(C)andC.__class__answer the metaclass.Other changes
D0-goldenchanges.C2-metaclasseswith 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 callsintas the metaclass and reports "int() takes at most 2 arguments (3 given)". The exception type matches.__get__on class access.__qualname__,__mro__, and the__module__/__qualname__entries CPython puts in the namespace.Testing
class D(5), now fixed;62-class-surfaceandC2re-run clean.🤖 Generated with Claude Code