Warn about if x is None == y is None
#6559
Labels
Good first issue
Friendly and approachable by new contributors
Hacktoberfest
Help wanted 🙏
Outside help would be appreciated, good for new contributors
Needs PR
This issue is accepted, sufficiently specified and now needs an implementation
Optional Checkers
Related to a checked, disabled by default
Milestone
Current problem
We sometimes see code like this:
It is very easy to make the mistake of writing the check as:
This actually is a chained comparison:
(left is None != right is None)
is equivalent to:...which is unsatisfiable, since
right is None
impliesnot(None != right)
.Desired solution
According to the
comparison
rule in the Python grammar (https://docs.python.org/3/reference/grammar.html),these comparison operators have the same precedence, and would lead to chained comparisons:
in, not in, is, is not, <, <=, >, >=, !=, ==
There are three groups:
{'in', 'not in'}
,{'is', 'is not'}
,{'<', '<=', '>', '>=', '!=', '=='}
.If the linter warned about chained comparisons where one expression is part of two comparisons that belong to different groups, this would flag up checks such as "left is None != right is None".
The price to pay is that this would also trigger on code such as...:
...but overall, it might be justified in those rare cases where a conditional just like that is appropriate to expect the author to silence the linter on that line.
The text was updated successfully, but these errors were encountered: