tonybaloney/perflint

W8401 (use-list-comprehension) with walrus operator

fabien-michel opened this issue · 0 comments

I'm not sure the W8401 should trigger when a walrus operator is implied in the condition.

The warning appear for this code.

def odd_only(a):
    if a % 2:
        return a
    return False

filtered = []
for a in range(1, 5):
    if b := odd_only(a):
        filtered.append(b)

Which should be changed to

filtered = [b for a in range(1, 5) if (b := odd_only(a))]

I'm found this kind of code not clear, because of the walrus operator.

The workaround is to not use the walrus operator:

filtered = []
for a in range(1, 5):
    b = odd_only(a)
    if b:
        filtered.append(b)

So, as there is an equivalence, and the warn is not triggered, it should also not warn when a walrus operator is used.