jendrikseipp / vulture

Find dead Python code
MIT License
3.38k stars 148 forks source link

AttributeError when parsing a decorator #283

Closed Llandy3d closed 1 year ago

Llandy3d commented 2 years ago

Hello,

I just wanted to raise awareness about an issue that I mainly encountered when working with the python prometheus client.

The get_decorator_name function seems to assume that after cycling over the ast.Attribute you can get the id decorator.id. When using a decorator from the prometheus_client for example:

from prometheus_client import Histogram
hist = Histogram('name', 'description', labelnames=["label1"])

@hist.labels('place1').time()
def myfunc():
    ...

after the Attribute you will be finding an ast.Call object that won't have the id attribute and raise the AttributeError. From my understanding this is due to some extra decorating work that the library is doing, but possibly the vulture tool shouldn't fail with an error.

For now this has been solved by ignoring the file with the --exclude flag

The traceback:

Traceback (most recent call last):
  File "/usr/local/bin/vulture", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 689, in main
    vulture.scavenge(config["paths"], exclude=config["exclude"])
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 264, in scavenge
    self.scan(module_string, filename=module)
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 231, in scan
    self.visit(node)
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 645, in visit
    return self.generic_visit(node)
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 677, in generic_visit
    self.visit(item)
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 630, in visit
    visitor(node)
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 564, in visit_FunctionDef
    decorator_names = [
  File "/usr/local/lib/python3.10/site-packages/vulture/core.py", line 565, in <listcomp>
    utils.get_decorator_name(decorator)
  File "/usr/local/lib/python3.10/site-packages/vulture/utils.py", line 69, in get_decorator_name
    return "@" + ".".join(reversed(parts))
AttributeError: 'Call' object has no attribute 'id'
jendrikseipp commented 2 years ago

Thanks for the report! I tried to replicate the error, but failed. Even with the regression test executed on several Python versions (https://github.com/jendrikseipp/vulture/pull/284), the error doesn't occur. Could you try to prepare a pull request that fixes the error?

Llandy3d commented 2 years ago

Sorry, I opened this with a rush and copied the wrong snippet. I updated the snippet in the code above, the correct one would be:

from prometheus_client import Histogram

hist = Histogram("name", "description", labelnames=["label1"])

@hist.labels("place1").time()
def myfunc():
    pass

I will be away for some time so nothing short term, it will probably require more analysis

jendrikseipp commented 2 years ago

Thanks! I put your code into my test branch (#284) and now the error happens for Python 3.10. For older Python versions, the decorator simply raises a SyntaxError, so this needs a version-dependent fix. Would be great if you could take a stab at this when you find the time :-)

Llandy3d commented 2 years ago

Actually I had some time and played around with it, I will be opening a PR soon but I will be able to check comments on it next week!

I tried with python 3.9.12 locally but I still see the AttributeError 🤔

jendrikseipp commented 1 year ago

Fixed in #284.