quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

comparison_to_true recommends an anti-pattern. #74

Closed coady closed 2 years ago

coady commented 9 years ago

Comparing things to True the wrong way Per the PEP 8 Style Guide, the preferred ways to compare something to True are the patterns if cond is True: or if cond:.

Pep8 does not recommend if cond is True; it specifically says that's even worse than == True. Implicit booleans are recommended.

vogelsgesang commented 9 years ago

I just had a quick look at PEP8, and it states:

Yes: if greeting: No: if greeting == True: Worse: if greeting is True:

On the other hand, the pep8 tool/linter has the error message E712 (http://pep8.readthedocs.org/en/latest/intro.html#error-codes) with the description:

comparison to True should be ‘if cond is True:’ or ‘if cond:’

I am not really sure what we should do in order to resolve this conflict. @acoady: What would be your resolution? Stick to the Pep8 styleguide? @adewes: Your opinion?

programmdesign commented 9 years ago

I'd go with PEP8 here. If greeting. It's most concise. Christoph

vogelsgesang commented 9 years ago

With which PEP8? Both sources are called Pep8. One is the official PEP8 styleguide, the other one is a tool which checks code against the PEP8 styleguide.

programmdesign commented 9 years ago

"Yes: if greeting"

omz commented 9 years ago

Also, what is this supposed to mean (emphasis mine)?

The code below uses the PEP 8 preferred pattern of if cond:. This only works if the object, variable, or expression evaluates to a Boolean value of True or False.

I can't think of any expression that can't be used in a boolean context.

tucked commented 5 years ago

https://github.com/PyCQA/pycodestyle/issues/696