deadcode
deadcode copied to clipboard
[Bug] "AttributeError: 'Call' object has no attribute 'id'" when running on a specific decorator
Summary
I'm trying to use deadcode on our code base, and it crashes. I managed to reduce the error to a few lines, and find the cultprit:
@Class().function()
def a():
return "42"
Explanation
It fails on the decorator @Class().function()
, on deadcode/visitor/utils.py:63
, in get_decorator_name
Indeed in this case the variable decorator
doesn't have an id
attribute. It's a ast.Call
.
I managed to fix it by changing the line with:
if hasattr(decorator, "id"):
parts.append(decorator.id) # type: ignore
else:
parts.append(decorator.func.id)
But I'm unsure if it's the right fix 🤷
More details
- I'm using deadcode
2.4.1
- python 3.11.7
- I'm on macos
- the full stacktrace is:
Traceback (most recent call last):
File "/path/to/code/backend/.venv/bin/deadcode", line 8, in <module>
sys.exit(print_main())
^^^^^^^^^^^^
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/cli.py", line 41, in print_main
if result := main():
^^^^^^
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/cli.py", line 26, in main
unused_names = find_unused_names(filenames=filenames, args=args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/actions/find_unused_names.py", line 13, in find_unused_names
dead_code_visitor.visit_abstract_syntax_trees()
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 106, in visit_abstract_syntax_trees
self.visit(node)
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 503, in visit
self.visit(item)
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 469, in visit
visitor(node)
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 387, in visit_FunctionDef
decorator_names = [utils.get_decorator_name(decorator) for decorator in node.decorator_list] # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/dead_code_visitor.py", line 387, in <listcomp>
decorator_names = [utils.get_decorator_name(decorator) for decorator in node.decorator_list] # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/path/to/code/backend/.venv/lib/python3.11/site-packages/deadcode/visitor/utils.py", line 66, in get_decorator_name
parts.append(decorator.id) # type: ignore
^^^^^^^^^^^^
AttributeError: 'Call' object has no attribute 'id'