@@ -200,27 +200,12 @@ def iter_child_nodes(node, omit=None, _fields_order=_FieldsOrder()):
200200
201201
202202def 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
207205 elif isinstance (item , ast .Tuple ):
208206 return tuple (convert_to_value (i ) for i in item .elts )
209- elif isinstance (item , ast .Num ):
210- return item .n
211207 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 )
224209 else :
225210 return UnhandledKeyType ()
226211
@@ -517,8 +502,8 @@ def __init__(self, name, source, scope):
517502
518503 def _add_to_names (container ):
519504 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 )
522507
523508 if isinstance (source .value , (ast .List , ast .Tuple )):
524509 _add_to_names (source .value )
@@ -1229,25 +1214,34 @@ def isDocstring(self, node):
12291214 Determine if the given node is a docstring, as long as it is at the
12301215 correct place in the node tree.
12311216 """
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+ )
12341222
12351223 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
12421232
12431233 def handleNode (self , node , parent ):
12441234 if node is None :
12451235 return
12461236 if self .offset and getattr (node , 'lineno' , None ) is not None :
12471237 node .lineno += self .offset [0 ]
12481238 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+ ):
12511245 self .futuresAllowed = False
12521246 self .nodeDepth += 1
12531247 node ._pyflakes_depth = self .nodeDepth
@@ -1318,11 +1312,14 @@ def handleStringAnnotation(self, s, node, ref_lineno, ref_col_offset, err):
13181312
13191313 @in_annotation
13201314 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+ ):
13221319 # Defer handling forward annotation.
13231320 self .deferFunction (functools .partial (
13241321 self .handleStringAnnotation ,
1325- annotation .s ,
1322+ annotation .value ,
13261323 node ,
13271324 annotation .lineno ,
13281325 annotation .col_offset ,
@@ -1387,7 +1384,7 @@ def SUBSCRIPT(self, node):
13871384
13881385 def _handle_string_dot_format (self , node ):
13891386 try :
1390- placeholders = tuple (parse_format_string (node .func .value .s ))
1387+ placeholders = tuple (parse_format_string (node .func .value .value ))
13911388 except ValueError as e :
13921389 self .report (messages .StringDotFormatInvalidFormat , node , e )
13931390 return
@@ -1503,7 +1500,8 @@ def _add_key(fmtkey):
15031500 def CALL (self , node ):
15041501 if (
15051502 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
15071505 node .func .attr == 'format'
15081506 ):
15091507 self ._handle_string_dot_format (node )
@@ -1584,7 +1582,7 @@ def CALL(self, node):
15841582
15851583 def _handle_percent_format (self , node ):
15861584 try :
1587- placeholders = parse_percent_format (node .left .s )
1585+ placeholders = parse_percent_format (node .left .value )
15881586 except ValueError :
15891587 self .report (
15901588 messages .PercentFormatInvalidFormat ,
@@ -1663,13 +1661,16 @@ def _handle_percent_format(self, node):
16631661
16641662 if (
16651663 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+ )
16671668 ):
16681669 if positional and positional_count > 1 :
16691670 self .report (messages .PercentFormatExpectedSequence , node )
16701671 return
16711672
1672- substitution_keys = {k .s for k in node .right .keys }
1673+ substitution_keys = {k .value for k in node .right .keys }
16731674 extra_keys = substitution_keys - named
16741675 missing_keys = named - substitution_keys
16751676 if not positional and extra_keys :
@@ -1688,27 +1689,24 @@ def _handle_percent_format(self, node):
16881689 def BINOP (self , node ):
16891690 if (
16901691 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 )
16921694 ):
16931695 self ._handle_percent_format (node )
16941696 self .handleChildren (node )
16951697
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 :
16981700 fn = functools .partial (
16991701 self .handleStringAnnotation ,
1700- node .s ,
1702+ node .value ,
17011703 node ,
17021704 node .lineno ,
17031705 node .col_offset ,
17041706 messages .ForwardAnnotationSyntaxError ,
17051707 )
17061708 self .deferFunction (fn )
17071709
1708- def CONSTANT (self , node ):
1709- if isinstance (node .value , str ):
1710- return self .STR (node )
1711-
17121710 # "slice" type nodes
17131711 SLICE = EXTSLICE = INDEX = handleChildren
17141712
0 commit comments