Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have PyLint flag 'Yoda conditions'?

Tags:

python

pylint

In some Python code I'm working on I spot here and there so-called 'Yoda conditions'.

Examples are:

if 0 < len(someList): ...

if None != ComputeSomething(): ...

Is there a way to have them flagged by PyLint?

like image 981
Jir Avatar asked Sep 14 '25 15:09

Jir


1 Answers

Pylint currently implements this as convention check C0122, starting with v1.5.0 (2015-11-29):

Add a new convention message, ‘misplaced-comparison-constant’, emitted when a constant is placed in the left hand side of a comparison, as in ‘5 == func()’. This is also called Yoda condition, since the flow of code reminds of the Star Wars green character, conditions usually encountered in languages with variabile assignments in conditional statements.

From the features documentation:

misplaced-comparison-constant (C0122):

Comparison should be %s Used when the constant is placed on the left side of a comparison. It is usually clearer in intent to place it in the right hand side of the comparison.

Example output:

C:130, 3: Comparison should be __name__ == '__main__' (misplaced-comparison-constant)
like image 130
Brian Cline Avatar answered Sep 17 '25 06:09

Brian Cline