jendrikseipp / vulture

Find dead Python code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError when parsing a decorator

Llandy3d opened this issue · comments

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'

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

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

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 :-)

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 🤔

Fixed in #284.