[Python-ideas] Warning on conditionals

Peter Otten __peter__ at web.de
Tue Dec 17 14:46:58 CET 2013


f p wrote:

> Hi, I'm new here,
> 
> There are cases of bad use of int or str constants without comparison
> operators in the if statement as shown in this example:
> 
> http://stackoverflow.com/questions/20633818/python-if-elif-always-not-
working.
> 
> Maybe the case in which the if statement is defeated should rate a
> warning.

When a newbie writes

if animal == "cat" or "dog":
    ...

it is clear that he has the wrong idea about how this is evaluated, but in 
the general case

if animal == cat or animal_barks:
    ...

this is not an error. A check would be of very limited use.
That said, there is an external tool that issues a warning:

$ cat tmp.py
import random
a = random.choice("ab")
if a == "b" or "c":
    print "yes"
else:
    assert False, "unreachable"
$ pychecker tmp.py
Processing module tmp (tmp.py)...
yes

Warnings...

tmp.py:3: Using a conditional statement with a constant value (c)




More information about the Python-ideas mailing list