Skip to content

Commit f14ba14

Browse files
Michael0x2agvanrossum
authored andcommitted
Add comments and some TODOs to fastparse2 (#2107)
This commit adds some documentation providing context into why fastparse2 exists for future readers, and adds a few missing TODOs flagging potential places for improvement.
1 parent d9c1062 commit f14ba14

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mypy/fastparse2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
"""
2+
This file is nearly identical to `fastparse.py`, except that it works with a Python 2
3+
AST instead of a Python 3 AST.
4+
5+
Previously, how we handled Python 2 code was by first obtaining the Python 2 AST via
6+
typed_ast, converting it into a Python 3 AST by using typed_ast.conversion, then
7+
running it through mypy.fastparse.
8+
9+
While this worked, it did add some overhead, especially in larger Python 2 codebases.
10+
This module allows us to skip the conversion step, saving us some time.
11+
12+
The reason why this file is not easily merged with mypy.fastparse despite the large amount
13+
of redundancy is because the Python 2 AST and the Python 3 AST nodes belong to two completely
14+
different class heirarchies, which made it difficult to write a shared visitor between the
15+
two in a typesafe way.
16+
"""
117
from functools import wraps
218
import sys
319

@@ -512,6 +528,8 @@ def visit_Print(self, n: ast27.Print) -> Node:
512528
if not n.nl:
513529
keywords.append(ast27.keyword("end", ast27.Str(" ", lineno=n.lineno, col_offset=-1)))
514530

531+
# TODO: Rather then desugaring Print into an intermediary ast27.Call object, it might
532+
# be more efficient to just directly create a mypy.node.CallExpr object.
515533
call = ast27.Call(
516534
ast27.Name("print", ast27.Load(), lineno=n.lineno, col_offset=-1),
517535
n.values, keywords, None, None,
@@ -528,6 +546,7 @@ def visit_Exec(self, n: ast27.Exec) -> Node:
528546
if new_locals is None:
529547
new_locals = ast27.Name("None", ast27.Load(), lineno=-1, col_offset=-1)
530548

549+
# TODO: Comment in visit_Print also applies here
531550
return self.visit(ast27.Expr(
532551
ast27.Call(
533552
ast27.Name("exec", ast27.Load(), lineno=n.lineno, col_offset=-1),
@@ -538,6 +557,7 @@ def visit_Exec(self, n: ast27.Exec) -> Node:
538557

539558
@with_line
540559
def visit_Repr(self, n: ast27.Repr) -> Node:
560+
# TODO: Comment in visit_Print also applies here
541561
return self.visit(ast27.Call(
542562
ast27.Name("repr", ast27.Load(), lineno=n.lineno, col_offset=-1),
543563
n.value,

0 commit comments

Comments
 (0)