@@ -1285,14 +1285,15 @@ def visit_NameConstant(self, n: NameConstant) -> Type:
1285
1285
else :
1286
1286
return UnboundType (str (n .value ), line = self .line )
1287
1287
1288
+ # Only for 3.8 and newer
1288
1289
def visit_Constant (self , n : Constant ) -> Type :
1289
1290
val = n .value
1290
1291
if val is None :
1291
1292
# None is a type.
1292
1293
return UnboundType (str (val ), line = self .line )
1293
1294
if isinstance (val , str ):
1294
1295
# Parse forward reference.
1295
- if (hasattr ( n , ' kind' ) and 'u' in n .kind ) or self .assume_str_is_unicode :
1296
+ if (n . kind and 'u' in n .kind ) or self .assume_str_is_unicode :
1296
1297
return parse_type_string (n .s , 'builtins.unicode' , self .line , n .col_offset ,
1297
1298
assume_str_is_unicode = self .assume_str_is_unicode )
1298
1299
else :
@@ -1324,10 +1325,6 @@ def visit_UnaryOp(self, n: UnaryOp) -> Type:
1324
1325
return typ
1325
1326
return self .invalid_type (n )
1326
1327
1327
- # Num(number n)
1328
- def visit_Num (self , n : Num ) -> Type :
1329
- return self .numeric_type (n .n , n )
1330
-
1331
1328
def numeric_type (self , value : object , n : AST ) -> Type :
1332
1329
# The node's field has the type complex, but complex isn't *really*
1333
1330
# a parent of int and float, and this causes isinstance below
@@ -1349,24 +1346,31 @@ def numeric_type(self, value: object, n: AST) -> Type:
1349
1346
column = getattr (n , 'col_offset' , - 1 ),
1350
1347
)
1351
1348
1352
- # Str(string s)
1353
- def visit_Str (self , n : Str ) -> Type :
1354
- # Note: we transform these fallback types into the correct types in
1355
- # 'typeanal.py' -- specifically in the named_type_with_normalized_str method.
1356
- # If we're analyzing Python 3, that function will translate 'builtins.unicode'
1357
- # into 'builtins.str'. In contrast, if we're analyzing Python 2 code, we'll
1358
- # translate 'builtins.bytes' in the method below into 'builtins.str'.
1359
- if 'u' in n .kind or self .assume_str_is_unicode :
1360
- return parse_type_string (n .s , 'builtins.unicode' , self .line , n .col_offset ,
1361
- assume_str_is_unicode = self .assume_str_is_unicode )
1362
- else :
1363
- return parse_type_string (n .s , 'builtins.str' , self .line , n .col_offset ,
1364
- assume_str_is_unicode = self .assume_str_is_unicode )
1349
+ if sys .version_info < (3 , 8 ):
1350
+ # Using typed_ast
1351
+
1352
+ # Num(number n)
1353
+ def visit_Num (self , n : Num ) -> Type :
1354
+ return self .numeric_type (n .n , n )
1355
+
1356
+ # Str(string s)
1357
+ def visit_Str (self , n : Str ) -> Type :
1358
+ # Note: we transform these fallback types into the correct types in
1359
+ # 'typeanal.py' -- specifically in the named_type_with_normalized_str method.
1360
+ # If we're analyzing Python 3, that function will translate 'builtins.unicode'
1361
+ # into 'builtins.str'. In contrast, if we're analyzing Python 2 code, we'll
1362
+ # translate 'builtins.bytes' in the method below into 'builtins.str'.
1363
+ if 'u' in n .kind or self .assume_str_is_unicode :
1364
+ return parse_type_string (n .s , 'builtins.unicode' , self .line , n .col_offset ,
1365
+ assume_str_is_unicode = self .assume_str_is_unicode )
1366
+ else :
1367
+ return parse_type_string (n .s , 'builtins.str' , self .line , n .col_offset ,
1368
+ assume_str_is_unicode = self .assume_str_is_unicode )
1365
1369
1366
- # Bytes(bytes s)
1367
- def visit_Bytes (self , n : Bytes ) -> Type :
1368
- contents = bytes_to_human_readable_repr (n .s )
1369
- return RawExpressionType (contents , 'builtins.bytes' , self .line , column = n .col_offset )
1370
+ # Bytes(bytes s)
1371
+ def visit_Bytes (self , n : Bytes ) -> Type :
1372
+ contents = bytes_to_human_readable_repr (n .s )
1373
+ return RawExpressionType (contents , 'builtins.bytes' , self .line , column = n .col_offset )
1370
1374
1371
1375
# Subscript(expr value, slice slice, expr_context ctx)
1372
1376
def visit_Subscript (self , n : ast3 .Subscript ) -> Type :
0 commit comments