tonybaloney / perflint

Python Linter for performance anti patterns
MIT License
659 stars 10 forks source link

W8401 (use-list-comprehension) with walrus operator #43

Closed fabien-michel closed 1 year ago

fabien-michel commented 1 year ago

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.