Skip to content

Commit

Permalink
If a class attribute is used, do not report any global by the same na…
Browse files Browse the repository at this point in the history
…me being used as well.
  • Loading branch information
johndoknjas committed Oct 5, 2024
1 parent d21ad35 commit 6c757d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion vulture/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def handle_syntax_error(e):
)
self.exit_code = ExitCode.InvalidInput
else:
utils.set_parent_info(node)
# When parsing type comments, visiting can throw SyntaxError.
try:
self.visit(node)
Expand Down Expand Up @@ -473,6 +474,10 @@ def ignored(lineno):
last_node = last_node or first_node
typ = collection.typ
first_lineno = lines.get_first_line_number(first_node)
if isinstance(
first_node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Name)
) and (class_part_of := utils.get_class(first_node)):
name = f"{class_part_of.name}.{name}"

if ignored(first_lineno):
self._log(f'Ignoring {typ} "{name}"')
Expand Down Expand Up @@ -509,7 +514,7 @@ def visit_Attribute(self, node):
if isinstance(node.ctx, ast.Store):
self._define(self.defined_attrs, node.attr, node)
elif isinstance(node.ctx, ast.Load):
self.used_names.add(node.attr)
self.used_names.add(f"{node.value.id}.{node.attr}")

def visit_BinOp(self, node):
"""
Expand Down
15 changes: 15 additions & 0 deletions vulture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ def read_file(filename):
raise VultureInputException from err


def set_parent_info(tree: ast.AST):
tree.parent = None
for node in ast.walk(tree):
for child in ast.iter_child_nodes(node):
child.parent = node


def get_class(node: ast.AST) -> ast.ClassDef | None:
while node is not None:
if isinstance(node, ast.ClassDef):
break
node = node.parent
return node


class LoggingList(list):
def __init__(self, typ, verbose):
self.typ = typ
Expand Down

0 comments on commit 6c757d5

Please sign in to comment.