@@ -200,27 +200,12 @@ def iter_child_nodes(node, omit=None, _fields_order=_FieldsOrder()):
200
200
201
201
202
202
def convert_to_value (item ):
203
- if isinstance (item , ast .Str ):
204
- return item .s
205
- elif hasattr (ast , 'Bytes' ) and isinstance (item , ast .Bytes ):
206
- return item .s
203
+ if isinstance (item , ast .Constant ):
204
+ return item .value
207
205
elif isinstance (item , ast .Tuple ):
208
206
return tuple (convert_to_value (i ) for i in item .elts )
209
- elif isinstance (item , ast .Num ):
210
- return item .n
211
207
elif isinstance (item , ast .Name ):
212
- result = VariableKey (item = item )
213
- constants_lookup = {
214
- 'True' : True ,
215
- 'False' : False ,
216
- 'None' : None ,
217
- }
218
- return constants_lookup .get (
219
- result .name ,
220
- result ,
221
- )
222
- elif isinstance (item , ast .NameConstant ):
223
- return item .value
208
+ return VariableKey (item = item )
224
209
else :
225
210
return UnhandledKeyType ()
226
211
@@ -517,8 +502,8 @@ def __init__(self, name, source, scope):
517
502
518
503
def _add_to_names (container ):
519
504
for node in container .elts :
520
- if isinstance (node , ast .Str ):
521
- self .names .append (node .s )
505
+ if isinstance (node , ast .Constant ) and isinstance ( node . value , str ):
506
+ self .names .append (node .value )
522
507
523
508
if isinstance (source .value , (ast .List , ast .Tuple )):
524
509
_add_to_names (source .value )
@@ -1229,25 +1214,34 @@ def isDocstring(self, node):
1229
1214
Determine if the given node is a docstring, as long as it is at the
1230
1215
correct place in the node tree.
1231
1216
"""
1232
- return isinstance (node , ast .Str ) or (isinstance (node , ast .Expr ) and
1233
- isinstance (node .value , ast .Str ))
1217
+ return (
1218
+ isinstance (node , ast .Expr ) and
1219
+ isinstance (node .value , ast .Constant ) and
1220
+ isinstance (node .value .value , str )
1221
+ )
1234
1222
1235
1223
def getDocstring (self , node ):
1236
- if isinstance (node , ast .Expr ):
1237
- node = node .value
1238
- if not isinstance (node , ast .Str ):
1239
- return (None , None )
1240
-
1241
- return (node .s , node .lineno - 1 )
1224
+ if (
1225
+ isinstance (node , ast .Expr ) and
1226
+ isinstance (node .value , ast .Constant ) and
1227
+ isinstance (node .value .value , str )
1228
+ ):
1229
+ return node .value .value , node .lineno - 1
1230
+ else :
1231
+ return None , None
1242
1232
1243
1233
def handleNode (self , node , parent ):
1244
1234
if node is None :
1245
1235
return
1246
1236
if self .offset and getattr (node , 'lineno' , None ) is not None :
1247
1237
node .lineno += self .offset [0 ]
1248
1238
node .col_offset += self .offset [1 ]
1249
- if self .futuresAllowed and not (isinstance (node , ast .ImportFrom ) or
1250
- self .isDocstring (node )):
1239
+ if (
1240
+ self .futuresAllowed and
1241
+ self .nodeDepth == 0 and
1242
+ not isinstance (node , ast .ImportFrom ) and
1243
+ not self .isDocstring (node )
1244
+ ):
1251
1245
self .futuresAllowed = False
1252
1246
self .nodeDepth += 1
1253
1247
node ._pyflakes_depth = self .nodeDepth
@@ -1318,11 +1312,14 @@ def handleStringAnnotation(self, s, node, ref_lineno, ref_col_offset, err):
1318
1312
1319
1313
@in_annotation
1320
1314
def handleAnnotation (self , annotation , node ):
1321
- if isinstance (annotation , ast .Str ):
1315
+ if (
1316
+ isinstance (annotation , ast .Constant ) and
1317
+ isinstance (annotation .value , str )
1318
+ ):
1322
1319
# Defer handling forward annotation.
1323
1320
self .deferFunction (functools .partial (
1324
1321
self .handleStringAnnotation ,
1325
- annotation .s ,
1322
+ annotation .value ,
1326
1323
node ,
1327
1324
annotation .lineno ,
1328
1325
annotation .col_offset ,
@@ -1387,7 +1384,7 @@ def SUBSCRIPT(self, node):
1387
1384
1388
1385
def _handle_string_dot_format (self , node ):
1389
1386
try :
1390
- placeholders = tuple (parse_format_string (node .func .value .s ))
1387
+ placeholders = tuple (parse_format_string (node .func .value .value ))
1391
1388
except ValueError as e :
1392
1389
self .report (messages .StringDotFormatInvalidFormat , node , e )
1393
1390
return
@@ -1503,7 +1500,8 @@ def _add_key(fmtkey):
1503
1500
def CALL (self , node ):
1504
1501
if (
1505
1502
isinstance (node .func , ast .Attribute ) and
1506
- isinstance (node .func .value , ast .Str ) and
1503
+ isinstance (node .func .value , ast .Constant ) and
1504
+ isinstance (node .func .value .value , str ) and
1507
1505
node .func .attr == 'format'
1508
1506
):
1509
1507
self ._handle_string_dot_format (node )
@@ -1584,7 +1582,7 @@ def CALL(self, node):
1584
1582
1585
1583
def _handle_percent_format (self , node ):
1586
1584
try :
1587
- placeholders = parse_percent_format (node .left .s )
1585
+ placeholders = parse_percent_format (node .left .value )
1588
1586
except ValueError :
1589
1587
self .report (
1590
1588
messages .PercentFormatInvalidFormat ,
@@ -1663,13 +1661,16 @@ def _handle_percent_format(self, node):
1663
1661
1664
1662
if (
1665
1663
isinstance (node .right , ast .Dict ) and
1666
- all (isinstance (k , ast .Str ) for k in node .right .keys )
1664
+ all (
1665
+ isinstance (k , ast .Constant ) and isinstance (k .value , str )
1666
+ for k in node .right .keys
1667
+ )
1667
1668
):
1668
1669
if positional and positional_count > 1 :
1669
1670
self .report (messages .PercentFormatExpectedSequence , node )
1670
1671
return
1671
1672
1672
- substitution_keys = {k .s for k in node .right .keys }
1673
+ substitution_keys = {k .value for k in node .right .keys }
1673
1674
extra_keys = substitution_keys - named
1674
1675
missing_keys = named - substitution_keys
1675
1676
if not positional and extra_keys :
@@ -1688,27 +1689,24 @@ def _handle_percent_format(self, node):
1688
1689
def BINOP (self , node ):
1689
1690
if (
1690
1691
isinstance (node .op , ast .Mod ) and
1691
- isinstance (node .left , ast .Str )
1692
+ isinstance (node .left , ast .Constant ) and
1693
+ isinstance (node .left .value , str )
1692
1694
):
1693
1695
self ._handle_percent_format (node )
1694
1696
self .handleChildren (node )
1695
1697
1696
- def STR (self , node ):
1697
- if self ._in_annotation :
1698
+ def CONSTANT (self , node ):
1699
+ if isinstance ( node . value , str ) and self ._in_annotation :
1698
1700
fn = functools .partial (
1699
1701
self .handleStringAnnotation ,
1700
- node .s ,
1702
+ node .value ,
1701
1703
node ,
1702
1704
node .lineno ,
1703
1705
node .col_offset ,
1704
1706
messages .ForwardAnnotationSyntaxError ,
1705
1707
)
1706
1708
self .deferFunction (fn )
1707
1709
1708
- def CONSTANT (self , node ):
1709
- if isinstance (node .value , str ):
1710
- return self .STR (node )
1711
-
1712
1710
# "slice" type nodes
1713
1711
SLICE = EXTSLICE = INDEX = handleChildren
1714
1712
0 commit comments