-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Semantic Analyzer
The mypy semantic analyzer binds names to definitions, builds symbol tables, and performs various simple consistency checks. It takes an abstract syntax tree as input and modifies it.
The semantic analyzer is implemented in several modules (mypy/semanal*.py
). mypy/semanal.py does the bulk of work, while analysis of types is performed in mypy/typeanal.py.
The semantic analyzer uses the visitor pattern.
The semantic analysis has several passes. Some checks depend on previous passes having populated certain information. mypy/semanal_main.py puts it all together and invokes 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.