I'm trying to clean up some tangled legacy code in django. Vulture looks like a good bet, but it doesn't seem to know how to find view functions referenced from urls.py. This isn't too surprising, since most of the functions are included as strings:
...
url(r'^some-url/$', 'my_app.views.some_url_view'),
...
Is there a simple way to make vulture aware of the view functions?
One thing I've got going for me: I've written fairly extensive tests for the code. Currently, vulture misses these tests, but if there were some way to make vulture aware of these tests, I think all the views would be included as well.
Is there a simple way to make vulture aware of the view functions?
Yes, there is - Vulture calls this process as "Whitelisting". The basic idea is to explicitly "use" the code Vulture reports as unused. This can be done by creating a mock object for unused code. Since it is a fairly common practice to create such mock objects, Vulture recently started to ship an abstract class vulture.whitelist_utils.Whitelist
for this very specific purpose.
For the example view function you gave, the whitelist may look like this:
# whitelists/whitelist_view.py
from vulture.whitelist_utils import Whitelist
view_whitelist = Whitelist()
# Create an attribute named exactly as your
# unused object - a function in this case
view_whitelist.some_url_view
Note that you will also have to pass this file as an argument to Vulture for it to work. In the above example, assuming that you are running Vulture on the apps
and tests
directories, if the whitelist was saved as whitelists/whitelist_view.py
, then the command for vulture should be:
vulture apps/ tests/ whitelists/
How does that work?
Since you also passed the whitelist file along with the file to be analysed, vulture created ast
’s for both of them and while parsing those abstract syntax trees, Vulture created a common set for storing the names of used and defined objects. Since the name of the false positive function occurs in both of them, it is therefore not treated as unused.
For more ways on how to minimise false positives, please go through Vulture's Documentation. There's also this blog post by Rahul on Vulture and false positives.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With