28
28
from mypy import experiments
29
29
from mypy import messages
30
30
from mypy .errors import Errors
31
+ from mypy .options import Options
31
32
32
33
try :
33
34
from typed_ast import ast3
58
59
59
60
60
61
def parse (source : Union [str , bytes ], fnam : str = None , errors : Errors = None ,
61
- pyversion : Tuple [ int , int ] = defaults . PYTHON3_VERSION ,
62
- custom_typing_module : str = None ) -> MypyFile :
62
+ options : Options = Options ()) -> MypyFile :
63
+
63
64
"""Parse a source file, without doing any semantic analysis.
64
65
65
66
Return the parse tree. If errors is not provided, raise ParseError
66
67
on failure. Otherwise, use the errors object to report parse errors.
67
-
68
- The pyversion (major, minor) argument determines the Python syntax variant.
69
68
"""
70
69
raise_on_error = False
71
70
if errors is None :
@@ -74,14 +73,16 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
74
73
errors .set_file ('<input>' if fnam is None else fnam , None )
75
74
is_stub_file = bool (fnam ) and fnam .endswith ('.pyi' )
76
75
try :
77
- assert pyversion [0 ] >= 3 or is_stub_file
78
- feature_version = pyversion [1 ] if not is_stub_file else defaults .PYTHON3_VERSION [1 ]
76
+ if is_stub_file :
77
+ feature_version = defaults .PYTHON3_VERSION [1 ]
78
+ else :
79
+ assert options .python_version [0 ] >= 3
80
+ feature_version = options .python_version [1 ]
79
81
ast = ast3 .parse (source , fnam , 'exec' , feature_version = feature_version )
80
82
81
- tree = ASTConverter (pyversion = pyversion ,
83
+ tree = ASTConverter (options = options ,
82
84
is_stub = is_stub_file ,
83
85
errors = errors ,
84
- custom_typing_module = custom_typing_module ,
85
86
).visit (ast )
86
87
tree .path = fnam
87
88
tree .is_stub = is_stub_file
@@ -136,17 +137,15 @@ def is_no_type_check_decorator(expr: ast3.expr) -> bool:
136
137
137
138
class ASTConverter (ast3 .NodeTransformer ): # type: ignore # typeshed PR #931
138
139
def __init__ (self ,
139
- pyversion : Tuple [ int , int ] ,
140
+ options : Options ,
140
141
is_stub : bool ,
141
- errors : Errors ,
142
- custom_typing_module : str = None ) -> None :
142
+ errors : Errors ) -> None :
143
143
self .class_nesting = 0
144
144
self .imports = [] # type: List[ImportBase]
145
145
146
- self .pyversion = pyversion
146
+ self .options = options
147
147
self .is_stub = is_stub
148
148
self .errors = errors
149
- self .custom_typing_module = custom_typing_module
150
149
151
150
def fail (self , msg : str , line : int , column : int ) -> None :
152
151
self .errors .report (line , column , msg )
@@ -260,12 +259,8 @@ def translate_module_id(self, id: str) -> str:
260
259
261
260
For example, translate '__builtin__' in Python 2 to 'builtins'.
262
261
"""
263
- if id == self .custom_typing_module :
262
+ if id == self .options . custom_typing_module :
264
263
return 'typing'
265
- elif id == '__builtin__' and self .pyversion [0 ] == 2 :
266
- # HACK: __builtin__ in Python 2 is aliases to builtins. However, the implementation
267
- # is named __builtin__.py (there is another layer of translation elsewhere).
268
- return 'builtins'
269
264
return id
270
265
271
266
def visit_Module (self , mod : ast3 .Module ) -> MypyFile :
@@ -855,16 +850,13 @@ def visit_Num(self, n: ast3.Num) -> Union[IntExpr, FloatExpr, ComplexExpr]:
855
850
# Str(string s)
856
851
@with_line
857
852
def visit_Str (self , n : ast3 .Str ) -> Union [UnicodeExpr , StrExpr ]:
858
- if self .pyversion [0 ] >= 3 or self .is_stub :
859
- # Hack: assume all string literals in Python 2 stubs are normal
860
- # strs (i.e. not unicode). All stubs are parsed with the Python 3
861
- # parser, which causes unprefixed string literals to be interpreted
862
- # as unicode instead of bytes. This hack is generally okay,
863
- # because mypy considers str literals to be compatible with
864
- # unicode.
865
- return StrExpr (n .s )
866
- else :
867
- return UnicodeExpr (n .s )
853
+ # Hack: assume all string literals in Python 2 stubs are normal
854
+ # strs (i.e. not unicode). All stubs are parsed with the Python 3
855
+ # parser, which causes unprefixed string literals to be interpreted
856
+ # as unicode instead of bytes. This hack is generally okay,
857
+ # because mypy considers str literals to be compatible with
858
+ # unicode.
859
+ return StrExpr (n .s )
868
860
869
861
# Only available with typed_ast >= 0.6.2
870
862
if hasattr (ast3 , 'JoinedStr' ):
@@ -894,11 +886,7 @@ def visit_Bytes(self, n: ast3.Bytes) -> Union[BytesExpr, StrExpr]:
894
886
# The following line is a bit hacky, but is the best way to maintain
895
887
# compatibility with how mypy currently parses the contents of bytes literals.
896
888
contents = str (n .s )[2 :- 1 ]
897
-
898
- if self .pyversion [0 ] >= 3 :
899
- return BytesExpr (contents )
900
- else :
901
- return StrExpr (contents )
889
+ return BytesExpr (contents )
902
890
903
891
# NameConstant(singleton value)
904
892
def visit_NameConstant (self , n : ast3 .NameConstant ) -> NameExpr :
0 commit comments