[Python-ideas] Warning on conditionals

MRAB python at mrabarnett.plus.com
Tue Dec 17 16:14:29 CET 2013


On 17/12/2013 13:46, Peter Otten wrote:
> 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)
>
It's the kind of thing that you could have in IDLE.



More information about the Python-ideas mailing list