Skip to content

MethodDeclaration

A Python instance method declaration component.

Shared base props for all method-like declarations.

import { MethodDeclaration } from "@alloy-js/python";
<MethodDeclaration abstract async decorators={Children[]} />
abstractoptional booleanIndicates that the method is abstract.
asyncoptional booleanIndicates that the function is async.
decoratorsoptional Children[]Decorators rendered above def, in source order — decorators[0] is topmost. By Python’s bottom-up application order, the topmost entry is the outermost decorator (applied last) and wraps the result of every decorator below it. Each entry should produce a complete decorator line (typically starting with @). Falsy entries (other than 0) are skipped, so conditional decorators can be provided inline when needed. When used through wrappers that emit an intrinsic decorator (ClassMethodDeclaration@classmethod, StaticMethodDeclaration@staticmethod, PropertyDeclaration@property), these decorators are rendered above the intrinsic line — the correct position for Pydantic’s @field_validator / @model_validator and other wrappers that must see the underlying function, not a descriptor. When used on plain MethodDeclaration / FunctionDeclaration, these decorators are rendered above @abstractmethod (if abstract is set) and above def. Do not pass intrinsic decorators here — i.e. @classmethod, @staticmethod, @property, or @abstractmethod. Those are emitted by the matching component (ClassMethodDeclaration, StaticMethodDeclaration, PropertyDeclaration, or the abstract flag) and would otherwise be stacked twice in the output, producing invalid Python.

Automatically injects the self parameter for instance methods and enforces that the declaration appears within a class body.

<py.MethodDeclaration name="do_work" parameters={[{ name: "value", type: "int" }]}>
  return value * 2
</py.MethodDeclaration>

Generates:

def do_work(self, value: int) -> None:
    return value * 2