-
Notifications
You must be signed in to change notification settings - Fork 0
Semantic Analyzer
The mypy semantic analyzer binds names to definitions and performs various simple consistency checks. It takes an abstract syntax tree as input and modified it.
The semantic analyzer is implemented in two modules. mypy/semanal.py is the main module, while analysis of types is performed in mypy/typeanal.py. Both of them use the visitor pattern.
TODO: Discuss the different passes.
The semantic analyzer binds references to local variables and global (module-level) definitions. Attribute references (of form x.y) are generally only bound during type checking, since binding them requires static type information. As an exception, attribute references via module objects are bound during semantic analysis.
The top level (outside any function or class definitions) of each module is a single (name) scope. The body of each class is also a single scope (not including any methods or nested classes). Each function is also a single scope (again nested scopes are separate). This means that compound statements do not introduce new scopes. This is similar to Python.
Each name can only have a single binding within a single scope. Multiple nested scopes can, however, have different definitions for a single name.
The SemanticAnalyzer
class maintains a per-module symbol table (the globals
attribute). After semantic analysis it is stored as the names
attribute of the relevant mypy.nodes.MypyFile
class.
Symbol tables for local scopes (i.e. functions) are stored in the locals
attribute of SemanticAnalyzer
.
The parser can process a single file without knowledge of any other modules. The semantic analyzer also needs to have access to the symbol tables of imported modules.