@@ -65,7 +65,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
65
65
is_stub_file = bool (fnam ) and fnam .endswith ('.pyi' )
66
66
try :
67
67
assert pyversion [0 ] >= 3 or is_stub_file
68
- ast = ast3 .parse (source , fnam , 'exec' )
68
+ ast = ast3 .parse (source , fnam , 'exec' , feature_version = pyversion [ 1 ] )
69
69
70
70
tree = ASTConverter (pyversion = pyversion ,
71
71
is_stub = is_stub_file ,
@@ -120,7 +120,7 @@ def is_no_type_check_decorator(expr: ast3.expr) -> bool:
120
120
return False
121
121
122
122
123
- class ASTConverter (ast3 .NodeTransformer ):
123
+ class ASTConverter (ast3 .NodeTransformer ): # type: ignore
124
124
def __init__ (self ,
125
125
pyversion : Tuple [int , int ],
126
126
is_stub : bool ,
@@ -392,7 +392,13 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
392
392
if no_type_check :
393
393
arg_type = None
394
394
else :
395
- arg_type = TypeConverter (self .errors , line = line ).visit (arg .annotation )
395
+ if arg .annotation is not None and arg .type_comment is not None :
396
+ self .fail (messages .DUPLICATE_TYPE_SIGNATURES , arg .lineno , arg .col_offset )
397
+ arg_type = None
398
+ if arg .annotation is not None :
399
+ arg_type = TypeConverter (self .errors , line = line ).visit (arg .annotation )
400
+ elif arg .type_comment is not None :
401
+ arg_type = parse_type_comment (arg .type_comment , arg .lineno , arg .col_offset )
396
402
return Argument (Var (arg .arg ), arg_type , self .visit (default ), kind )
397
403
398
404
new_args = []
@@ -487,28 +493,24 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
487
493
# Assign(expr* targets, expr? value, string? type_comment, expr? annotation)
488
494
@with_line
489
495
def visit_Assign (self , n : ast3 .Assign ) -> AssignmentStmt :
490
- typ = None
491
- if hasattr (n , 'annotation' ) and n .annotation is not None : # type: ignore
492
- new_syntax = True
493
- else :
494
- new_syntax = False
495
- if new_syntax and self .pyversion < (3 , 6 ):
496
- self .fail ('Variable annotation syntax is only supported in Python 3.6, '
497
- 'use type comment instead' , n .lineno , n .col_offset )
498
- # typed_ast prevents having both type_comment and annotation.
496
+ lvalues = self .translate_expr_list (n .targets )
497
+ rvalue = self .visit (n .value )
499
498
if n .type_comment is not None :
500
499
typ = parse_type_comment (n .type_comment , n .lineno , self .errors )
501
- elif new_syntax :
502
- typ = TypeConverter (self .errors , line = n .lineno ).visit (n .annotation ) # type: ignore
503
- typ .column = n .annotation .col_offset
500
+ else :
501
+ typ = None
502
+ return AssignmentStmt (lvalues , rvalue , type = typ , new_syntax = False )
503
+
504
+ # AnnAssign(expr target, expr annotation, expr? value, int simple)
505
+ @with_line
506
+ def visit_AnnAssign (self , n : ast3 .AnnAssign ) -> AssignmentStmt :
504
507
if n .value is None : # always allow 'x: int'
505
508
rvalue = TempNode (AnyType ()) # type: Expression
506
509
else :
507
510
rvalue = self .visit (n .value )
508
- lvalues = self .translate_expr_list (n .targets )
509
- return AssignmentStmt (lvalues ,
510
- rvalue ,
511
- type = typ , new_syntax = new_syntax )
511
+ typ = TypeConverter (self .errors , line = n .lineno ).visit (n .annotation )
512
+ typ .column = n .annotation .col_offset
513
+ return AssignmentStmt ([self .visit (n .target )], rvalue , type = typ , new_syntax = True )
512
514
513
515
# AugAssign(expr target, operator op, expr value)
514
516
@with_line
@@ -815,9 +817,6 @@ def is_star2arg(k: ast3.keyword) -> bool:
815
817
# Num(object n) -- a number as a PyObject.
816
818
@with_line
817
819
def visit_Num (self , n : ast3 .Num ) -> Union [IntExpr , FloatExpr , ComplexExpr ]:
818
- if getattr (n , 'contains_underscores' , None ) and self .pyversion < (3 , 6 ):
819
- self .fail ('Underscores in numeric literals are only supported in Python 3.6' ,
820
- n .lineno , n .col_offset )
821
820
if isinstance (n .n , int ):
822
821
return IntExpr (n .n )
823
822
elif isinstance (n .n , float ):
@@ -914,7 +913,7 @@ def visit_Index(self, n: ast3.Index) -> Node:
914
913
return self .visit (n .value )
915
914
916
915
917
- class TypeConverter (ast3 .NodeTransformer ):
916
+ class TypeConverter (ast3 .NodeTransformer ): # type: ignore
918
917
def __init__ (self , errors : Errors , line : int = - 1 ) -> None :
919
918
self .errors = errors
920
919
self .line = line
0 commit comments