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

AttributeError when parsing a decorator #283

Closed
Llandy3d opened this issue Jul 5, 2022 · 5 comments
Closed

AttributeError when parsing a decorator #283

Llandy3d opened this issue Jul 5, 2022 · 5 comments

Comments

@Llandy3d
Copy link

Llandy3d commented Jul 5, 2022

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
Copy link
Owner

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?

@Llandy3d
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

Fixed in #284.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants