flake8-simplify
flake8-simplify copied to clipboard
[Adjust Rule] SIM102 - comments in between if clauses
Desired change
- Rule(s): SIM102
-
Adjustment: If a comment is placed between
if
conditions, don't flag this rule
Explanation
Sometimes it is clearer to split up logic into separate if
conditions with comments explaining what is going on under the if
conditions
Example
This is an example where the mentioned rule(s) would currently be suboptimal:
for p in reversed(path.parents):
...
if p == path.parent:
# This is the last iteration
if p in some_list and some_bool:
do_something()
With the current rule, I guess you can do this:
for p in reversed(path.parents):
...
if (
p == path.parent
# This is the last iteration
and
p in some_list and some_bool
):
do_something()
but it is less readable in my opinion.
I would try to get rid of the comment, e.g. as:
for p in reversed(path.parents):
...
is_last_iteration = p == path.parent
if is_last_iteration and p in some_list and some_bool:
do_something()
@MetRonnie Do you have an example where getting rid of the comments would not be desirable?
Mainly my example in the OP. Your suggestion of creating a variable feels a bit inefficient to me, and I could imagine scenarios where a lengthier comment that couldn't be summarised with a variable (although admittedly I haven't run into any since opening the issue).