project_ananthu project_ananthu - 1 month ago 9
Python Question

How to find hidden errors in Python?

This is a simple python code. If the value of

a
is changed then there will be an error showing that
b
is undefined. The
else
condition acting like a hidden condition that only gives the error when the interpreter runs that line of code.

How can I detect these kind of errors in Python?

a=0
if a==0 :
print "hello"
else :
print b

Answer

pylint is able to find a lot of common (and not so common) errors in python code without executing it, example output from your example:

C:  1, 0: Exactly one space required around assignment
a=0
 ^ (bad-whitespace)
C:  2, 0: Exactly one space required around comparison
if a==0 :
    ^^ (bad-whitespace)
C:  2, 0: No space allowed before :
if a==0 :
        ^ (bad-whitespace)
C:  4, 0: No space allowed before :
else :
     ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
E:  5,10: Undefined variable 'b' (undefined-variable)

The line you're interested in here is: E: 5,10: Undefined variable 'b' (undefined-variable)