module documentation

Top-level logic for the semantic analyzer. The semantic analyzer binds names, resolves imports, detects various special constructs that don't have dedicated AST nodes after parse (such as 'cast' which looks like a call), populates symbol tables, and performs various simple consistency checks. Semantic analysis of each SCC (strongly connected component; import cycle) is performed in one unit. Each module is analyzed as multiple separate *targets*; the module top level is one target and each function is a target. Nested functions are not separate targets, however. This is mostly identical to targets used by mypy daemon (but classes aren't targets in semantic analysis). We first analyze each module top level in an SCC. If we encounter some names that we can't bind because the target of the name may not have been processed yet, we *defer* the current target for further processing. Deferred targets will be analyzed additional times until everything can be bound, or we reach a maximum number of iterations. We keep track of a set of incomplete namespaces, i.e. namespaces that we haven't finished populating yet. References to these namespaces cause a deferral if they can't be satisfied. Initially every module in the SCC will be incomplete.

Function apply_class_plugin_hooks Apply class plugin hooks within a SCC.
Function apply_hooks_to_class Undocumented
Function calculate_class_properties Undocumented
Function check_blockers Undocumented
Function check_type_arguments Undocumented
Function check_type_arguments_in_targets Check type arguments against type variable bounds and restrictions.
Function cleanup_builtin_scc Remove imported names from builtins namespace.
Function get_all_leaf_targets Return all leaf targets in a symbol table (module-level and methods).
Function process_functions Undocumented
Function process_top_level_function Analyze single top-level function or method.
Function process_top_levels Undocumented
Function restore_saved_attrs Restore instance variables removed during AST strip that haven't been added yet.
Function semantic_analysis_for_scc Perform semantic analysis for all modules in a SCC (import cycle).
Function semantic_analysis_for_targets Semantically analyze only selected nodes in a given module.
Function semantic_analyze_target Semantically analyze a single target.
Constant core_modules Undocumented
Constant CORE_WARMUP Undocumented
Constant MAX_ITERATIONS Undocumented
Type Alias Patches Undocumented
Type Alias TargetInfo Undocumented
def apply_class_plugin_hooks(graph: Graph, scc: list[str], errors: Errors): (source)

Apply class plugin hooks within a SCC. We run these after to the main semantic analysis so that the hooks don't need to deal with incomplete definitions such as placeholder types. Note that some hooks incorrectly run during the main semantic analysis pass, for historical reasons.

def apply_hooks_to_class(self: SemanticAnalyzer, module: str, info: TypeInfo, options: Options, file_node: MypyFile, errors: Errors) -> bool: (source)

Undocumented

def calculate_class_properties(graph: Graph, scc: list[str], errors: Errors): (source)

Undocumented

def check_blockers(graph: Graph, scc: list[str]): (source)

Undocumented

def check_type_arguments(graph: Graph, scc: list[str], errors: Errors): (source)

Undocumented

def check_type_arguments_in_targets(targets: list[FineGrainedDeferredNode], state: State, errors: Errors): (source)

Check type arguments against type variable bounds and restrictions. This mirrors the logic in check_type_arguments() except that we process only some targets. This is used in fine grained incremental mode.

def cleanup_builtin_scc(state: State): (source)

Remove imported names from builtins namespace. This way names imported from typing in builtins.pyi aren't available by default (without importing them). We can only do this after processing the whole SCC is finished, when the imported names aren't needed for processing builtins.pyi itself.

def get_all_leaf_targets(file: MypyFile) -> list[TargetInfo]: (source)

Return all leaf targets in a symbol table (module-level and methods).

def process_functions(graph: Graph, scc: list[str], patches: Patches): (source)

Undocumented

def process_top_level_function(analyzer: SemanticAnalyzer, state: State, module: str, target: str, node: (FuncDef|OverloadedFuncDef)|Decorator, active_type: TypeInfo|None, patches: Patches): (source)

Analyze single top-level function or method. Process the body of the function (including nested functions) again and again, until all names have been resolved (or iteration limit reached).

def process_top_levels(graph: Graph, scc: list[str], patches: Patches): (source)

Undocumented

def restore_saved_attrs(saved_attrs: SavedAttributes): (source)

Restore instance variables removed during AST strip that haven't been added yet.

def semantic_analysis_for_scc(graph: Graph, scc: list[str], errors: Errors): (source)

Perform semantic analysis for all modules in a SCC (import cycle). Assume that reachability analysis has already been performed. The scc will be processed roughly in the order the modules are included in the list.

def semantic_analysis_for_targets(state: State, nodes: list[FineGrainedDeferredNode], graph: Graph, saved_attrs: SavedAttributes): (source)

Semantically analyze only selected nodes in a given module. This essentially mirrors the logic of semantic_analysis_for_scc() except that we process only some targets. This is used in fine grained incremental mode, when propagating an update. The saved_attrs are implicitly declared instance attributes (attributes defined on self) removed by AST stripper that may need to be reintroduced here. They must be added before any methods are analyzed.

def semantic_analyze_target(target: str, state: State, node: ((MypyFile|FuncDef)|OverloadedFuncDef)|Decorator, active_type: TypeInfo|None, final_iteration: bool, patches: Patches) -> tuple[list[str], bool, bool]: (source)

Semantically analyze a single target. Return tuple with these items: - list of deferred targets - was some definition incomplete (need to run another pass) - were any new names defined (or placeholders replaced)

core_modules: list[str] = (source)

Undocumented

Value
['typing',
 '_collections_abc',
 'builtins',
 'abc',
 'collections',
 'collections.abc']
CORE_WARMUP: int = (source)

Undocumented

Value
2
MAX_ITERATIONS: int = (source)

Undocumented

Value
20
Patches: _TypeAlias = (source)

Undocumented

Value
List[Tuple[int, Callable[[], None]]]