Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with name conflicts between attributes and globals #372

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading