Skip to content

Commit

Permalink
Use ast.Constant instead of ast.Str.
Browse files Browse the repository at this point in the history
  • Loading branch information
jendrikseipp committed Oct 6, 2023
1 parent 7382260 commit 652aa73
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,11 @@ def visit_BinOp(self, node):
"%(my_var)s" % locals()
"""
if (
isinstance(node.left, ast.Str)
utils.is_ast_string(node.left)
and isinstance(node.op, ast.Mod)
and self._is_locals_call(node.right)
):
self.used_names |= set(re.findall(r"%\((\w+)\)", node.left.s))
self.used_names |= set(re.findall(r"%\((\w+)\)", node.left.value))

def visit_Call(self, node):
# Count getattr/hasattr(x, "some_attr", ...) as usage of some_attr.
Expand All @@ -531,21 +531,21 @@ def visit_Call(self, node):
or (node.func.id == "hasattr" and len(node.args) == 2)
):
attr_name_arg = node.args[1]
if isinstance(attr_name_arg, ast.Str):
self.used_names.add(attr_name_arg.s)
if utils.is_ast_string(attr_name_arg):
self.used_names.add(attr_name_arg.value)

# Parse variable names in new format strings:
# "{my_var}".format(**locals())
if (
isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Str)
and utils.is_ast_string(node.func.value)
and node.func.attr == "format"
and any(
kw.arg is None and self._is_locals_call(kw.value)
for kw in node.keywords
)
):
self._handle_new_format_string(node.func.value.s)
self._handle_new_format_string(node.func.value.value)

def _handle_new_format_string(self, s):
def is_identifier(name):
Expand Down Expand Up @@ -651,8 +651,8 @@ def visit_Assign(self, node):
if _assigns_special_variable__all__(node):
assert isinstance(node.value, (ast.List, ast.Tuple))
for elt in node.value.elts:
if isinstance(elt, ast.Str):
self.used_names.add(elt.s)
if utils.is_ast_string(elt):
self.used_names.add(elt.value)

def visit_While(self, node):
self._handle_conditional_node(node, "while")
Expand Down
5 changes: 5 additions & 0 deletions vulture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def condition_is_always_true(condition):
return _safe_eval(condition, False)


def is_ast_string(node):
print("DIR", node, dir(node))
return isinstance(node, ast.Constant) and isinstance(node.value, str)


def format_path(path):
try:
return path.relative_to(pathlib.Path.cwd())
Expand Down

0 comments on commit 652aa73

Please sign in to comment.