ADR-0025: Compile a class method once for each class that has it¶
Status¶
Accepted
Date¶
2026-11-13
Context¶
CLS-06 asks for @classmethod. A class method takes the class it is
called through as its first parameter, cls. In CPython Shape.create()
passes Shape and Square.create() passes Square, so cls(...) makes
an instance of the class the call names, cls.name reads that class's
variable, and cls.other() calls that class's class method. Alternative
constructors, the most common use, depend on it.
A TyPy value has its type when the program is compiled (TYP-11). An
instance is made from a class known then (ADR-0012), and a class's
__init__ may take other parameters than its base's.
Options considered¶
- The class as a value when the program runs. One body takes
clsas a type object.cls(...)would need the class's layout and its__init__found when the program runs, and the arguments could not be checked against a subclass's__init__. - Bind
clsto the class that defines the method. One body, in whichclsis that class.Square.create()would make aShape, unlike CPython, unless calls through subclasses are refused. - One copy per class. Each class that has the class method, its own
or inherited, gets a copy whose
clsis that class, checked and compiled as an ordinary function. This is how ADR-0023 compiles generics.
Decision¶
TyPy uses option 3.
- Copies. The class that defines a class method, and each class of the
same module that derives from it and does not define it again, has a
copy named after the class, such as
Square.create. Each copy is checked withclsof the type of its class's type object, socls(...)is checked against that class's__init__, and an error names the class whose copy has it. - Calls.
Class.name(...)calls the copy of that class, directly. Inside a copy,cls(...)makes an instance of its class, andcls.nameandcls.name(...)read and call through its class. - Refused for now. Calling a class method through an instance, which in CPython passes the instance's class, needs dispatch on that class; and a class of another module that inherits a class method needs its copy checked in the defining module. Both are E0201, planned for 0.5.
Consequences¶
- A class method costs code once for each class that has it, and a call costs what a call of a function costs.
- A subclass whose
__init__does not fit the arguments a class method passes tocls(...)is reported where the copy is checked, rather than raisingTypeErrorwhen the program runs. - Copies for subclasses in other modules wait for the checker to check a body in the scope of a module already checked, which ADR-0023's generics need too.
Related requirements¶
CLS-06, TYP-11, ADR-0012, ADR-0021, ADR-0023.