Skip to content

PERF401 is triggered on for-loops that has the walrus operator inside #15047

@Majsvaffla

Description

@Majsvaffla

PERF401 is triggered on code that has the walrus operator inside. The walrus operator is not available in the list-comprehension if-shorthand and it can not be recreated within a single list-comprehension.

Minimal example

items = []

for i in range(5):
    if j := i:
        items.append(j)

This code can obviously be refactored to not include the walrus operator but there are more complex scenarios where it is impossible to do so without using multiple list-comprehensions or duplicating code.

More complex example

import re

items = []

for s in ["abc", "cde", "efg"]:
    if match := re.match(r"e[a-z]+", s):
        items.append(match.group(0))

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdocumentationImprovements or additions to documentationfixesRelated to suggested fixes for violations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions