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
+ """
1
17
from functools import wraps
2
18
import sys
3
19
@@ -512,6 +528,8 @@ def visit_Print(self, n: ast27.Print) -> Node:
512
528
if not n .nl :
513
529
keywords .append (ast27 .keyword ("end" , ast27 .Str (" " , lineno = n .lineno , col_offset = - 1 )))
514
530
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.
515
533
call = ast27 .Call (
516
534
ast27 .Name ("print" , ast27 .Load (), lineno = n .lineno , col_offset = - 1 ),
517
535
n .values , keywords , None , None ,
@@ -528,6 +546,7 @@ def visit_Exec(self, n: ast27.Exec) -> Node:
528
546
if new_locals is None :
529
547
new_locals = ast27 .Name ("None" , ast27 .Load (), lineno = - 1 , col_offset = - 1 )
530
548
549
+ # TODO: Comment in visit_Print also applies here
531
550
return self .visit (ast27 .Expr (
532
551
ast27 .Call (
533
552
ast27 .Name ("exec" , ast27 .Load (), lineno = n .lineno , col_offset = - 1 ),
@@ -538,6 +557,7 @@ def visit_Exec(self, n: ast27.Exec) -> Node:
538
557
539
558
@with_line
540
559
def visit_Repr (self , n : ast27 .Repr ) -> Node :
560
+ # TODO: Comment in visit_Print also applies here
541
561
return self .visit (ast27 .Call (
542
562
ast27 .Name ("repr" , ast27 .Load (), lineno = n .lineno , col_offset = - 1 ),
543
563
n .value ,
0 commit comments