-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Python Parser
Jukka Lehtosalo edited this page Jun 27, 2022
·
8 revisions
The mypy parser uses the CPython "ast" module to convert source code into an abstract syntax tree (AST). (The term parse tree is sometimes used informally as a synonym.)
You can manually call the mypy.parse.parse
function to experiment with it (first install test dependencies in the current virtualenv):
$ cd mypy # mypy repo
$ python
>>> from mypy.options import Options
>>> from mypy.parse import parse
>>> parse("print('hello, world')", "hello.py", None, None, Options())
MypyFile:1(
hello.py
ExpressionStmt:1(
CallExpr:1(
NameExpr(print)
Args(
StrExpr(hello, world)))))
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.