Quoth the specification,
Don't compare boolean values to True or False using ==.
Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
pycodestyle considers both the "yes" and the "worse" case equivalently good; if anything, it should complain more about the "Worse" case than the "Yes" one.
It also complains, I believe spuriously, about SQLAlchemy's overridden use of == as described in this Stack Overflow question.
Could this be restricted to just emitting the warning in the literal case of if ... == True:, or is True: which is probably redundant, and leaving more complex expressions alone?
I suspect that the Worse clause was added after the check was written. (That check has existed for years.)
I believe the specification doesn't scope this to just if statements. The examples use if statements but they intend for it to apply regardless of where it occurs. I agree it's a pain in the neck in this case and it could be improved to also flag greeting is True. (That may have regressed as I thought it used to flag that.)
I would like to take on this issue; however, I am not sure about how to deal with the not cases. Below is my current behavior. Should if x is not True: and related be allowed? I personally think if not x: styling is best, but I know several people who complain about the readability of this.
if x is True:
if x is False:
if x is None: Good
if True is x:
if False is x:
if None is x: Good
if x is not True:
if x is not False:
if x is not None: Good
if True is not x:
if False is not x:
if None is not x: Good
We had a lint warning on if x is True in $large_organisation, and I saw it cause people to break code by rewriting correct code like
if x is True:
do_something()
elif x > 0:
do_something_else()
to
if x:
do_something()
elif x > 0:
do_something_else()
The case PEP-8 says is "Worse" above has different semantics, and pycodestyle shouldn't recommend changes that make a semantic difference to code.
Most helpful comment
We had a lint warning on
if x is Truein$large_organisation, and I saw it cause people to break code by rewriting correct code liketo
The case PEP-8 says is "Worse" above has different semantics, and pycodestyle shouldn't recommend changes that make a semantic difference to code.