Steven
Steven

Reputation: 2114

How to detect a declared variable that is never used?

In Python, I think a common potential error is to misspell a variable name on assignment, then when you wish to use the correctly spelled version of the variable, you don't get the behavior you anticipated. For example:

my_variable = "Hello"
my_varible = "World"
print (my_variable)

The intent was to assign a new value to my_variable, but because there was a typo, the program prints "Hello" when the intent was "World". I have tried ``pylint,pychecker, andpyflakes`, but either I'm using the tools incorrectly, or they don't provide a warning for this scenario.

So the question is, does a publicly available tool exist that will detect unused variables that have been assigned once and return a warning? The ideal tool would tell me that I declared my_varible at line 2 and never used it later.

Upvotes: 0

Views: 851

Answers (2)

Steven
Steven

Reputation: 2114

As posted by davedwards in comments, the solution to my question was to use vulture. On Ubuntu, I ran pip install vulture, then just ran vulture against my test code and received this:

vulture misspelled.py

misspelled.py:2: unused variable 'my_varible' (60% confidence)

vulture is perfect for me because if a script has no errors, it produces no output. Since it is a command-line tool, I can automate use of this tool over a large code base.

dave, if you return to this question and post the same answer, I will delete this answer and mark yours as the preferred answer.

Upvotes: 1

Giftlin Rajaiah
Giftlin Rajaiah

Reputation: 11

PyCharm IDE shows warnings for typos and unused variables.

Upvotes: 0

Related Questions