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[]}
/>
basesoptional Children[]The base classes that this class inherits from.
childrenoptional Children
docoptional ChildrenDocumentation for this declaration
namestring | NamekeyThe base name of this declaration. May change depending on naming policy and any conflicts.
refkeyoptional Refkey | 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