@@ -307,8 +307,13 @@ def visit_BinOp(self, node: ast.BinOp) -> None:
307307
308308 "%(my_var)s" % locals()
309309 """
310- if isinstance (node .left , ast .Str ) and isinstance (node .op , ast .Mod ) and self ._is_locals_call (node .right ):
311- self .used_names |= set (re .findall (r'%\((\w+)\)' , node .left .s ))
310+ if (
311+ isinstance (node .left , ast .Constant )
312+ and isinstance (node .left .value , str )
313+ and isinstance (node .op , ast .Mod )
314+ and self ._is_locals_call (node .right )
315+ ):
316+ self .used_names |= set (re .findall (r'%\((\w+)\)' , node .left .value ))
312317
313318 def visit_Call (self , node : ast .Call ) -> None :
314319 # Count getattr/hasattr(x, "some_attr", ...) as usage of some_attr.
@@ -317,18 +322,22 @@ def visit_Call(self, node: ast.Call) -> None:
317322 or (node .func .id == 'hasattr' and len (node .args ) == 2 )
318323 ):
319324 attr_name_arg = node .args [1 ]
320- if isinstance (attr_name_arg , ast .Str ):
321- self .add_used_name (attr_name_arg .s )
325+ if (
326+ isinstance (attr_name_arg , ast .Constant )
327+ and isinstance (attr_name_arg .value , str )
328+ ):
329+ self .add_used_name (attr_name_arg .value )
322330
323331 # Parse variable names in new format strings:
324332 # "{my_var}".format(**locals())
325333 if (
326334 isinstance (node .func , ast .Attribute )
327- and isinstance (node .func .value , ast .Str )
335+ and isinstance (node .func .value , ast .Constant )
336+ and isinstance (node .func .value .value , str )
328337 and node .func .attr == 'format'
329338 and any (kw .arg is None and self ._is_locals_call (kw .value ) for kw in node .keywords )
330339 ):
331- self ._handle_new_format_string (node .func .value .s )
340+ self ._handle_new_format_string (node .func .value .value )
332341
333342 def _handle_new_format_string (self , s : str ) -> None :
334343 def is_identifier (name : str ) -> bool :
@@ -428,8 +437,8 @@ def visit_Assign(self, node: ast.Assign) -> None:
428437 if _assigns_special_variable__all__ (node ):
429438 assert isinstance (node .value , (ast .List , ast .Tuple ))
430439 for elt in node .value .elts :
431- if isinstance (elt , ast .Str ):
432- self .add_used_name (elt .s )
440+ if isinstance (elt , ast .Constant ) and isinstance ( elt . value , str ):
441+ self .add_used_name (elt .value )
433442
434443 def visit_While (self , node : ast .While ) -> None :
435444 self ._handle_conditional_node (node , 'while' )
0 commit comments