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 | TargetInfo |
Undocumented |
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[ | Undocumented |
state:State | Undocumented |
errors:Errors | Undocumented |
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:State | Undocumented |
Parameters | |
file:MypyFile | Undocumented |
Returns | |
List[ | Undocumented |
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:SemanticAnalyzer | Undocumented |
state:State | Undocumented |
module:str | Undocumented |
target:str | Undocumented |
node:Union[ | Undocumented |
active_type:Optional[ | Undocumented |
patches:Patches | Undocumented |
Parameters | |
saved_attrs:SavedAttributes | Undocumented |
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:State | Undocumented |
nodes:List[ | Undocumented |
graph:Graph | Undocumented |
saved_attrs:SavedAttributes | Undocumented |
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:str | Undocumented |
state:State | Undocumented |
node:Union[ | Undocumented |
active_type:Optional[ | Undocumented |
final_iteration:bool | Undocumented |
patches:Patches | Undocumented |
Returns | |
Tuple[ | Undocumented |