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[]} />import { MethodDeclaration } from "@alloy-js/python/stc";
MethodDeclaration({ abstract: boolean, async: boolean, decorators: Children[],}).children(children)| abstract | optional boolean | Indicates that the method is abstract. |
| async | optional boolean | Indicates that the function is async. |
| decorators | optional 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. |
Remarks
Section titled “Remarks”Automatically injects the self parameter for instance methods and enforces that the declaration appears within a class body.
Example
Section titled “Example”<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