FunctionDeclaration
A Python function declaration.
import { FunctionDeclaration } from "@alloy-js/python";
<FunctionDeclaration async decorators={Children[]} />import { FunctionDeclaration } from "@alloy-js/python/stc";
FunctionDeclaration({ async: boolean, decorators: Children[],}).children(children)| 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”This component creates a Python function declaration with optional type annotations, parameters, and return types. It supports async functions and automatically handles symbol creation and emission.
Example
Section titled “Example”<FunctionDeclaration
name="my_function"
returnType="int"
parameters={[{ name: "a", type: { children: "int" } }, { name: "b", type: { children: "str" } }]}
>
return a + b
</FunctionDeclaration>
This will generate:
def my_function(a: int, b: str) -> int:
return a + b