You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shantanu edited this page Sep 12, 2023
·
8 revisions
The mypy parser uses the CPython "ast" module to convert source code into an abstract syntax tree (AST). The mypy parser then translates the CPython AST into a mypy AST defined in mypy/nodes.py.
(The term parse tree is sometimes used informally as a synonym of AST.)
We don't use the CPython AST directly since it doesn't contain all the node types and attributes mypy needs. Having a custom AST implementation also gives us more flexibility and is more efficient.
You can manually call the mypy.parse.parse function to experiment with it (first install test dependencies in the current virtualenv):
The names MypyFile, ExpressionStmt, CallExpr, NameExpr and StrExpr refer to AST node classes defined in mypy/nodes.py.
The numbers after colons are line numbers.
The parser does only a minimal amount of consistency checks. As such it also accepts many invalid programs.
The next compiler pass, Semantic Analyzer, performs additional checks.