How to find unused code on a Python website?

We have been using Django for a long time. Some old code is not in use right now. How can I find which code is no longer in use and delete.

I used coverage.py with unit tests, which works great and shows which part of the code is never used, but the tested test is very low. Is there a way to use it with a WSGI server to find which code has never served web requests?

+5
source share
2 answers

It depends on what you mean by unused code.

For unreachable dead code, such functions have never been called; classes that have never been created, you can use a pure static code analyzer to find them. Pylint is a good option. Keep in mind that this is not 100% accurate, a false positive is possible:

 # static analysis can't detect methods called this way func = getattr(obj, "func_name") func() 

For code that is accessible but not reached. You should rely on tools like coverage.py and improve your testing coverage.

+1
source

In a well-tested project, coverage would be ideal, but with some untested obsolete code, I don't think there is a magic tool.

You can write a great test by loading all pages and running coverage to get some guidance.


Cowboy style:

If this is not some kind of critical code, and you are sure that it is not used (i.e. does not process payments, etc.). Comment on this, make sure the tests pass, deploy, and wait a week or so before deleting them specifically (or returning them if you received a notification).

0
source

All Articles