@@ -65,7 +65,7 @@ def parse(source: Union[str, bytes], fnam: str = None, errors: Errors = None,
6565 is_stub_file = bool (fnam ) and fnam .endswith ('.pyi' )
6666 try :
6767 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 ] )
6969
7070 tree = ASTConverter (pyversion = pyversion ,
7171 is_stub = is_stub_file ,
@@ -120,7 +120,7 @@ def is_no_type_check_decorator(expr: ast3.expr) -> bool:
120120 return False
121121
122122
123- class ASTConverter (ast3 .NodeTransformer ):
123+ class ASTConverter (ast3 .NodeTransformer ): # type: ignore
124124 def __init__ (self ,
125125 pyversion : Tuple [int , int ],
126126 is_stub : bool ,
@@ -392,7 +392,13 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
392392 if no_type_check :
393393 arg_type = None
394394 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 )
396402 return Argument (Var (arg .arg ), arg_type , self .visit (default ), kind )
397403
398404 new_args = []
@@ -487,28 +493,24 @@ def visit_Delete(self, n: ast3.Delete) -> DelStmt:
487493 # Assign(expr* targets, expr? value, string? type_comment, expr? annotation)
488494 @with_line
489495 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 )
499498 if n .type_comment is not None :
500499 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 :
504507 if n .value is None : # always allow 'x: int'
505508 rvalue = TempNode (AnyType ()) # type: Expression
506509 else :
507510 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 )
512514
513515 # AugAssign(expr target, operator op, expr value)
514516 @with_line
@@ -815,9 +817,6 @@ def is_star2arg(k: ast3.keyword) -> bool:
815817 # Num(object n) -- a number as a PyObject.
816818 @with_line
817819 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 )
821820 if isinstance (n .n , int ):
822821 return IntExpr (n .n )
823822 elif isinstance (n .n , float ):
@@ -914,7 +913,7 @@ def visit_Index(self, n: ast3.Index) -> Node:
914913 return self .visit (n .value )
915914
916915
917- class TypeConverter (ast3 .NodeTransformer ):
916+ class TypeConverter (ast3 .NodeTransformer ): # type: ignore
918917 def __init__ (self , errors : Errors , line : int = - 1 ) -> None :
919918 self .errors = errors
920919 self .line = line
0 commit comments