generate_function_stub

generate_function_stub(sym: FunctionSymbol, ctx: StubContext) str[source]

Generate the .pyi stub line(s) for a module-level function.

Handles both synchronous and asynchronous functions. The async keyword is emitted when is_async is True or inspect.iscoroutinefunction() confirms it at runtime.

Parameter formatting follows the same inline / multi-line rules as generate_method_stub(): inline when ≤ 2 parameters, multi-line otherwise. Raw AST annotation strings are used where they preserve type-alias information that runtime evaluation would destroy.

Parameters:
Returns:

str – One or more stub lines.

Examples

>>> from stubpy.context import StubContext
>>> from stubpy.symbols import FunctionSymbol
>>> from stubpy.ast_pass import FunctionInfo
>>> fi = FunctionInfo(name="greet", lineno=1, raw_return_annotation="str")
>>> def greet(name: str) -> str: return name
>>> sym = FunctionSymbol("greet", 1, live_func=greet, ast_info=fi)
>>> "def greet" in generate_function_stub(sym, StubContext())
True

See also

Public API reference — overview of all public names.