module documentation
(source)

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 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
Variable ​Patches Undocumented
Variable ​Target​Info Undocumented
def calculate_class_properties(graph, scc, errors): (source)

Undocumented

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
errors:ErrorsUndocumented
def check_blockers(graph, scc): (source)

Undocumented

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
def check_type_arguments(graph, scc, errors): (source)

Undocumented

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
errors:ErrorsUndocumented
def check_type_arguments_in_targets(targets, state, 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.

Parameters
targets:List[FineGrainedDeferredNode]Undocumented
state:StateUndocumented
errors:ErrorsUndocumented
def cleanup_builtin_scc(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.

Parameters
state:StateUndocumented
def get_all_leaf_targets(file): (source)
Return all leaf targets in a symbol table (module-level and methods).
Parameters
file:MypyFileUndocumented
Returns
List[TargetInfo]Undocumented
def process_functions(graph, scc, patches): (source)

Undocumented

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
patches:PatchesUndocumented
def process_top_level_function(analyzer, state, module, target, node, active_type, 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).

Parameters
analyzer:SemanticAnalyzerUndocumented
state:StateUndocumented
module:strUndocumented
target:strUndocumented
node:Union[FuncDef, OverloadedFuncDef, Decorator]Undocumented
active​_type:Optional[TypeInfo]Undocumented
patches:PatchesUndocumented
def process_top_levels(graph, scc, patches): (source)

Undocumented

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
patches:PatchesUndocumented
def restore_saved_attrs(saved_attrs): (source)
Restore instance variables removed during AST strip that haven't been added yet.
Parameters
saved​_attrs:SavedAttributesUndocumented
def semantic_analysis_for_scc(graph, scc, 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.

Parameters
graph:GraphUndocumented
scc:List[str]Undocumented
errors:ErrorsUndocumented
def semantic_analysis_for_targets(state, nodes, graph, saved_attrs): (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.

Parameters
state:StateUndocumented
nodes:List[FineGrainedDeferredNode]Undocumented
graph:GraphUndocumented
saved​_attrs:SavedAttributesUndocumented
def semantic_analyze_target(target, state, node, active_type, final_iteration, patches): (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 were defined (or placeholders replaced)

Parameters
target:strUndocumented
state:StateUndocumented
node:Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator]Undocumented
active​_type:Optional[TypeInfo]Undocumented
final​_iteration:boolUndocumented
patches:PatchesUndocumented
Returns
Tuple[List[str], bool, bool]Undocumented
core_modules: list[str] = (source)

Undocumented

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

Undocumented

Value
2
MAX_ITERATIONS: int = (source)

Undocumented

Value
20
Patches: _TypeAlias = (source)

Undocumented

TargetInfo = (source)

Undocumented