generate_function_stub¶
- generate_function_stub(sym: FunctionSymbol, ctx: StubContext) str[source]¶
Generate the
.pyistub line(s) for a module-level function.Handles both synchronous and asynchronous functions. The
asynckeyword is emitted whenis_asyncisTrueorinspect.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:
sym (FunctionSymbol) – The function symbol from the
SymbolTable.ctx (StubContext) – The current
StubContext.
- 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.