generate_variable_stub

generate_variable_stub(sym: VariableSymbol, ctx: StubContext) str[source]

Generate a name: Type line for a module-level variable.

Resolution order for the type string:

  1. AST annotation string — the annotation as written in source.

  2. Inferred typetype(live_value).__name__ when the variable has no annotation. A WARNING diagnostic is recorded because the inferred type may be imprecise.

  3. Skip — returns "" if neither source is available.

Parameters:
Returns:

str – A single name: Type line, 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.