generate_alias_stub¶
- generate_alias_stub(sym: AliasSymbol, ctx: StubContext) str[source]¶
Re-emit a TypeVar, TypeAlias, NewType, ParamSpec, or TypeVarTuple declaration.
The source text is taken verbatim from the AST pre-pass (
ast_info) so that constraints, bounds, and alias expansions are preserved exactly as written.Emission format per kind is controlled by
alias_style:TypeAlias declarations (annotated, implicit bare, or PEP 695):
"compatible"(default) —Name: TypeAlias = <rhs>Works on all Python 3.10+ versions."pep695"—type Name = <rhs>Python 3.12+ only."auto"— usespep695when running on Python 3.12+, otherwise falls back tocompatible.
TypeVar / ParamSpec / TypeVarTuple / NewType always emit as
Name = Kind(...)regardless ofalias_style.- Parameters:
sym (AliasSymbol) – The alias symbol from the
SymbolTable.ctx (StubContext) – The current
StubContext.
- Returns:
str – One declaration line, or
""when insufficient AST data is available.
Examples
>>> from stubpy.context import StubContext >>> from stubpy.symbols import AliasSymbol >>> from stubpy.ast_pass import TypeVarInfo >>> tv = TypeVarInfo(name="T", lineno=1, kind="TypeVar", source_str="TypeVar('T')") >>> sym = AliasSymbol("T", lineno=1, ast_info=tv) >>> generate_alias_stub(sym, StubContext()) "T = TypeVar('T')"
See also
Public API reference — overview of all public names.