72
72
TYPE_COMMENT_AST_ERROR = 'invalid type comment or annotation'
73
73
74
74
75
+ # Older versions of typing don't allow using overload outside stubs,
76
+ # so provide a dummy.
77
+ if not MYPY and sys .version_info < (3 , 6 ):
78
+ def overload (x : Any ) -> Any : # noqa
79
+ return x
80
+
81
+
75
82
def parse (source : Union [str , bytes ],
76
83
fnam : str ,
77
84
module : Optional [str ],
@@ -887,42 +894,40 @@ def visit_Str(self, n: ast3.Str) -> Union[UnicodeExpr, StrExpr]:
887
894
# unicode.
888
895
return StrExpr (n .s )
889
896
890
- # Only available with typed_ast >= 0.6.2
891
- if hasattr (ast3 , 'JoinedStr' ):
892
- # JoinedStr(expr* values)
893
- @with_line
894
- def visit_JoinedStr (self , n : ast3 .JoinedStr ) -> Expression :
895
- # Each of n.values is a str or FormattedValue; we just concatenate
896
- # them all using ''.join.
897
- empty_string = StrExpr ('' )
898
- empty_string .set_line (n .lineno , n .col_offset )
899
- strs_to_join = ListExpr (self .translate_expr_list (n .values ))
900
- strs_to_join .set_line (empty_string )
901
- join_method = MemberExpr (empty_string , 'join' )
902
- join_method .set_line (empty_string )
903
- result_expression = CallExpr (join_method ,
904
- [strs_to_join ],
905
- [ARG_POS ],
906
- [None ])
907
- return result_expression
908
-
909
- # FormattedValue(expr value)
910
- @with_line
911
- def visit_FormattedValue (self , n : ast3 .FormattedValue ) -> Expression :
912
- # A FormattedValue is a component of a JoinedStr, or it can exist
913
- # on its own. We translate them to individual '{}'.format(value)
914
- # calls -- we don't bother with the conversion/format_spec fields.
915
- exp = self .visit (n .value )
916
- exp .set_line (n .lineno , n .col_offset )
917
- format_string = StrExpr ('{}' )
918
- format_string .set_line (n .lineno , n .col_offset )
919
- format_method = MemberExpr (format_string , 'format' )
920
- format_method .set_line (format_string )
921
- result_expression = CallExpr (format_method ,
922
- [exp ],
923
- [ARG_POS ],
924
- [None ])
925
- return result_expression
897
+ # JoinedStr(expr* values)
898
+ @with_line
899
+ def visit_JoinedStr (self , n : ast3 .JoinedStr ) -> Expression :
900
+ # Each of n.values is a str or FormattedValue; we just concatenate
901
+ # them all using ''.join.
902
+ empty_string = StrExpr ('' )
903
+ empty_string .set_line (n .lineno , n .col_offset )
904
+ strs_to_join = ListExpr (self .translate_expr_list (n .values ))
905
+ strs_to_join .set_line (empty_string )
906
+ join_method = MemberExpr (empty_string , 'join' )
907
+ join_method .set_line (empty_string )
908
+ result_expression = CallExpr (join_method ,
909
+ [strs_to_join ],
910
+ [ARG_POS ],
911
+ [None ])
912
+ return result_expression
913
+
914
+ # FormattedValue(expr value)
915
+ @with_line
916
+ def visit_FormattedValue (self , n : ast3 .FormattedValue ) -> Expression :
917
+ # A FormattedValue is a component of a JoinedStr, or it can exist
918
+ # on its own. We translate them to individual '{}'.format(value)
919
+ # calls -- we don't bother with the conversion/format_spec fields.
920
+ exp = self .visit (n .value )
921
+ exp .set_line (n .lineno , n .col_offset )
922
+ format_string = StrExpr ('{}' )
923
+ format_string .set_line (n .lineno , n .col_offset )
924
+ format_method = MemberExpr (format_string , 'format' )
925
+ format_method .set_line (format_string )
926
+ result_expression = CallExpr (format_method ,
927
+ [exp ],
928
+ [ARG_POS ],
929
+ [None ])
930
+ return result_expression
926
931
927
932
# Bytes(bytes s)
928
933
@with_line
@@ -1003,7 +1008,13 @@ def __init__(self, errors: Optional[Errors], line: int = -1) -> None:
1003
1008
self .line = line
1004
1009
self .node_stack = [] # type: List[ast3.AST]
1005
1010
1006
- def _visit_implementation (self , node : Optional [ast3 .AST ]) -> Optional [Type ]:
1011
+ @overload
1012
+ def visit (self , node : ast3 .expr ) -> Type : ...
1013
+
1014
+ @overload # noqa
1015
+ def visit (self , node : Optional [ast3 .AST ]) -> Optional [Type ]: ...
1016
+
1017
+ def visit (self , node : Optional [ast3 .AST ]) -> Optional [Type ]: # noqa
1007
1018
"""Modified visit -- keep track of the stack of nodes"""
1008
1019
if node is None :
1009
1020
return None
@@ -1013,19 +1024,6 @@ def _visit_implementation(self, node: Optional[ast3.AST]) -> Optional[Type]:
1013
1024
finally :
1014
1025
self .node_stack .pop ()
1015
1026
1016
- if sys .version_info >= (3 , 6 ):
1017
- @overload
1018
- def visit (self , node : ast3 .expr ) -> Type : ...
1019
-
1020
- @overload # noqa
1021
- def visit (self , node : Optional [ast3 .AST ]) -> Optional [Type ]: ...
1022
-
1023
- def visit (self , node : Optional [ast3 .AST ]) -> Optional [Type ]: # noqa
1024
- return self ._visit_implementation (node )
1025
- else :
1026
- def visit (self , node : Optional [ast3 .AST ]) -> Any :
1027
- return self ._visit_implementation (node )
1028
-
1029
1027
def parent (self ) -> Optional [ast3 .AST ]:
1030
1028
"""Return the AST node above the one we are processing"""
1031
1029
if len (self .node_stack ) < 2 :
0 commit comments