Skip to content

ClassDeclaration

Create a Python class declaration.

import { ClassDeclaration } from "@alloy-js/python";
<ClassDeclaration
bases={Children[]}
doc={Children}
name={string | Namekey}
refkey={Refkey | Refkey[]}
/>
basesoptionalChildren[]

The base classes that this class inherits from.

childrenoptionalChildren
docoptionalChildren

Documentation for this declaration

namestring | Namekey

The base name of this declaration. May change depending on naming policy and any conflicts.

refkeyoptionalRefkey | Refkey[]

The refkey or array of refkeys for this declaration.

Any child declarations (methods, fields, nested classes) will be placed in the class scope. This component creates a class scope to hold its members.

<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