flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

[Adjust Rule] SIM102 - comments in between if clauses

Open MetRonnie opened this issue 3 years ago • 3 comments

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.

MetRonnie avatar Jun 11 '21 13:06 MetRonnie

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()

MartinThoma avatar Mar 02 '22 12:03 MartinThoma

@MetRonnie Do you have an example where getting rid of the comments would not be desirable?

MartinThoma avatar Mar 28 '22 14:03 MartinThoma

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).

MetRonnie avatar Mar 28 '22 16:03 MetRonnie