MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

[Adjust Rule] SIM111 for compound conditionals #59

Closed hjoliver closed 3 years ago

hjoliver commented 3 years ago

Desired change

Explanation

The warning message is incorrect for compound expressions. It can be fixed by enclosing the conditional in parentheses.

Example

This is an example where the mentioned rule(s) would currently be suboptimal wrong:

for x in iterable:
    if A(x) and B(x):
        return False
return True

Result, with correction:

- SIM111: Use 'return all(not A(x) and B(x) for x in iterable)'
+ SIM111: Use 'return all(not (A(x) and B(x)) for x in iterable)'
                              ^             ^

or (but you probably don't want to be parsing the expression and doing boolean algebra?):

- SIM111: Use 'return all(not A(x) and B(x) for x in iterable)'
+ SIM111: Use 'return all(not A(x) or not B(x) for x in iterable)'