How can you find unused functions in Python code?
Asked Answered
Y

6

99

So you've got some legacy code lying around in a fairly hefty project. How can you find and delete dead functions?

I've seen these two references: Find unused code and Tool to find unused functions in php project, but they seem specific to C# and PHP, respectively.

Is there a Python tool that'll help you find functions that aren't referenced anywhere else in the source code (notwithstanding reflection/etc.)?

Yamauchi answered 28/3, 2009 at 16:42 Comment(0)
P
152

In Python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and figleaf. They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.

Alternatively, you can use static code analyzers that just look at your code, but don't actually run it. They run much faster, but due to Python's dynamic nature the results may contain false positives. Two tools in this category are pyflakes and vulture. Pyflakes finds unused imports and unused local variables. Vulture finds all kinds of unused and unreachable code. (Full disclosure: I'm the maintainer of Vulture.)

The tools are available in the Python Package Index https://pypi.org/.

Prissy answered 22/3, 2012 at 15:6 Comment(4)
+1 for vulture. I just pip installed it, ran it on my directory, and it gave the output I was looking for.Antonelli
+1 Just installed vulture, and I love it! Rust has unused code warnings are built-in by default, something I've since come to miss in Python. I especially love that vulture displays confidence level! Developers like you make the world a better place :-).Lott
I run vulture first then coverage.Strander
This is great! I've been looking for something like vulture.Lawabiding
C
11

Another option is deadcode Python package. It detects globally defined but unused variable, function and class names. This tool works as a static code checker: it first parses all globally defined names and then counts how many times are they referenced in a whole code base.

You can install it:

pip install deadcode

and run it

deadcode .

I have implemented this tool, since other ones didn't do the job for me. It should be convenient to use deadcode as part of CI/CD pipeline. I hope it helps.

Chiclayo answered 24/6, 2023 at 23:30 Comment(2)
Just tried this against vulture and vulture detected and unused variable that deadcode missed. Nice tool nonethelessRolandorolandson
deadcode worked great for meSargassum
B
4

I'm not sure if this is helpful, but you might try using the coverage, figleaf or other similar modules, which record which parts of your source code is used as you actually run your scripts/application.

Bayer answered 28/3, 2009 at 19:21 Comment(0)
T
3

Because of the fairly strict way python code is presented, would it be that hard to build a list of functions based on a regex looking for def function_name(..) ?

And then search for each name and tot up how many times it features in the code. It wouldn't naturally take comments into account but as long as you're having a look at functions with less than two or three instances...

It's a bit Spartan but it sounds like a nice sleepy-weekend task =)

Tann answered 28/3, 2009 at 17:11 Comment(0)
M
2

unless you know that your code uses reflection, as you said, I would go for a trivial grep. Do not underestimate the power of the asterisk in vim as well (performs a search of the word you have under your cursor in the file), albeit this is limited only to the file you are currently editing.

Another solution you could implement is to have a very good testsuite (seldomly happens, unfortunately) and then wrap the routine with a deprecation routine. if you get the deprecation output, it means that the routine was called, so it's still used somewhere. This works even for reflection behavior, but of course you can never be sure if you don't trigger the situation when your routine call is performed.

Miser answered 28/3, 2009 at 19:44 Comment(2)
I have my Vim config define key "<Leader>g" (ie. pressing comma then g, the comma thing being a Vim convention for user-defined keys) to run 'grep' across the codebase on the word under the cursor. It actually calls a ~/bin/grp script of mine, which invokes grep with some predefined '--exclude' and '--exclude-dir' - so that my command-line greps give the same results as my in-Vim greps. I defined 'grp' once, literally decades ago, and tweaked it once or twice since. Faster than awk, and as fast as ag, give or take, depending on circumstance.Antonelli
Having said all that, I wouldn't recommend this as an answer to the OP. You don't want to be doing a many big manual searches (one for each function) regularly. I'm disappointed pylint doesn't cover it, so I'm going to check out what vulture, etc, have to offer.Antonelli
C
0

its not only searching function names, but also all the imported packages not in use. you need to search the code for all the imported packages (including aliases) and search used functions, then create a list of the specific imports from each package (example instead of import os, replace with from os import listdir, getcwd,......)

Credence answered 26/5, 2020 at 20:54 Comment(1)
This is an okay technique when working with one- or two-file projects, but as the OP stated, they're looking for something that works with a "fairly hefty" project. Hence, going through and tracking down all the unused variables, functions, and module imports yourself is much too tedious and time-consuming.Lott

© 2022 - 2024 — McMap. All rights reserved.