StubContext

class StubContext(alias_registry: list[AliasEntry] = <factory>, type_module_imports: dict[str, str]=<factory>, used_type_imports: dict[str, str]=<factory>, config: StubConfig = <factory>, diagnostics: DiagnosticCollector = <factory>, symbol_table: Any | None = None, all_exports: set[str] | None = None, module_namespace: dict[str, ~typing.Any]=<factory>)[source]

Bases: object

Mutable state container scoped to one stub-generation run.

Create one instance per generate_stub() call, or pass a pre-configured instance to supply custom options.

alias_registry

Registered type aliases from imported sub-modules.

Type:

list of AliasEntry

type_module_imports

Import statements for alias sub-modules, keyed by local name.

Type:

dict

used_type_imports

Subset of type_module_imports actually referenced in the stub.

Type:

dict

config

Per-run options (execution mode, privacy, verbosity, etc.).

Type:

StubConfig

diagnostics

Accumulated warnings and errors from the pipeline.

Type:

DiagnosticCollector

symbol_table

Populated after the symbol-table stage; None until then.

Type:

SymbolTable or None

all_exports

Contents of __all__ from the target module, or None.

Type:

set of str or None

module_namespace

The full __dict__ of the loaded module, populated after stage 1 (module loading). Used by resolve_function_params() to look up forwarding-target callables by name. Empty in AST-only mode.

Type:

dict

Examples

>>> ctx = StubContext()
>>> ctx.diagnostics.summary()
'0 errors, 0 warnings, 0 infos'
>>> ctx.config.execution_mode.value
'runtime'
>>> ctx.symbol_table is None
True
alias_registry: list[AliasEntry]
type_module_imports: dict[str, str]
used_type_imports: dict[str, str]
config: StubConfig
diagnostics: DiagnosticCollector
symbol_table: Any | None = None
all_exports: set[str] | None = None
module_namespace: dict[str, Any]
lookup_alias(annotation: Any) str | None[source]

Return the alias string for annotation if registered, else None.

Examples

>>> ctx = StubContext()
>>> ctx.alias_registry.append(AliasEntry(str | int, "types.T"))
>>> ctx.type_module_imports["types"] = "from pkg import types"
>>> ctx.lookup_alias(str | int)
'types.T'
>>> ctx.lookup_alias(str | float) is None
True
__init__(alias_registry: list[AliasEntry] = <factory>, type_module_imports: dict[str, str]=<factory>, used_type_imports: dict[str, str]=<factory>, config: StubConfig = <factory>, diagnostics: DiagnosticCollector = <factory>, symbol_table: Any | None = None, all_exports: set[str] | None = None, module_namespace: dict[str, ~typing.Any]=<factory>) None

See also

Public API reference — overview of all public names.