generate_variable_stub¶
- generate_variable_stub(sym: VariableSymbol, ctx: StubContext) str[source]¶
Generate a
name: Typeline for a module-level variable.Resolution order for the type string:
AST annotation string — the annotation as written in source.
Inferred type —
type(live_value).__name__when the variable has no annotation. AWARNINGdiagnostic is recorded because the inferred type may be imprecise.Skip — returns
""if neither source is available.
- Parameters:
sym (VariableSymbol) – The variable symbol from the
SymbolTable.ctx (StubContext) – The current
StubContext.
- Returns:
str – A single
name: Typeline, or""when the type cannot be determined.
Examples
>>> from stubpy.context import StubContext >>> from stubpy.symbols import VariableSymbol >>> sym = VariableSymbol("MAX", 1, annotation_str="int", live_value=100) >>> generate_variable_stub(sym, StubContext()) 'MAX: int' >>> sym2 = VariableSymbol("FLAG", 2, live_value=True, inferred_type_str="bool") >>> generate_variable_stub(sym2, StubContext()) 'FLAG: bool'
See also
Public API reference — overview of all public names.