ClassDeclaration
Create a Python class declaration.
import { ClassDeclaration } from "@alloy-js/python";
<ClassDeclaration bases={Children[]} decorators={Children[]} doc={Children} name={string | Namekey} refkey={Refkey | Refkey[]} />import { ClassDeclaration } from "@alloy-js/python/stc";
ClassDeclaration({ bases: Children[], decorators: Children[], doc: Children, name: string | Namekey, refkey: Refkey | Refkey[],}).children(children)| bases | optional Children[] | The base classes that this class inherits from. |
| children | optional Children | |
| decorators | optional Children[] | Decorators rendered above the class keyword, in source order — decorators[0] is topmost. By Python’s bottom-up application order, the topmost entry is the outermost decorator (applied last). Each entry should produce a complete decorator line (typically starting with @). Falsy entries are skipped, so conditional decorators can be provided inline when needed. Wrappers like DataclassDeclaration build their intrinsic decorator (e.g. @dataclass(...)) and append it to this list at the position that keeps @dataclass closest to the class — i.e. user decorators end up above the intrinsic one, matching how Pydantic’s @field_validator sits above @classmethod on methods. Do not pass a wrapper’s intrinsic decorator here. For example, when using DataclassDeclaration, do not include @dataclass(...) in decorators — the component renders it for you, and stacking it twice would produce invalid Python. |
| doc | optional Children | Documentation for this declaration |
| name | string | Namekey | The base name of this declaration. May change depending on naming policy and any conflicts. |
| refkey | optional Refkey | Refkey[] | The refkey or array of refkeys for this declaration. |
Remarks
Section titled “Remarks”Any child declarations (methods, fields, nested classes) will be placed in the class scope. This component creates a class scope to hold its members.
Example
Section titled “Example”<ClassDeclaration name="MyClass" bases={["BaseClass"]}>
<VariableDeclaration name="a" type="int" />
<VariableDeclaration name="b" type="str" />
<py.FunctionDeclaration name="my_method" parameters={[{ name: "a", type: "int" }, { name: "b", type: "str" }]} returnType="int">
return a + b
</py.FunctionDeclaration>
</ClassDeclaration>
renders to
class MyClass(BaseClass):
a: int = None
b: str = None
def my_method(self, a: int, b: str) -> int:
return a + b