Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Shantanu edited this pageSep 12, 2023 ·8 revisions

The mypy parser uses the CPython "ast" module to convert source code into anabstract syntax tree (AST). The mypy parser then translates the CPython AST into a mypy AST defined inmypy/nodes.py.(The termparse 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 themypy.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>>> print(parse("print('hello, world')", "hello.py", None, None, Options()))MypyFile:1(  hello.py  ExpressionStmt:1(    CallExpr:1(      NameExpr(print)      Args(        StrExpr(hello, world)))))

The namesMypyFile,ExpressionStmt,CallExpr,NameExpr andStrExpr refer to AST node classes defined inmypy/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.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp