This rule raises an issue when the walrus operator is used in a way which makes the code confusing, as described in PEP 572.

Why is this an issue?

The walrus operator := (also known as "assignment expression") should be used with caution as it can easily make code more difficult to understand and thus maintain. In such case it is advised to refactor the code and use an assignment statement (i.e. =) instead.

Reasons why it is better to avoid using the walrus operator in Python:

How to fix it

Avoid using the walrus operator for the cases when it is not mandatory.

Code examples

Noncompliant code example

v0 = (v1 := f(p))  # Noncompliant: Use an assignment statement ("=") instead; ":=" operator is confusing in this context
f'{(x:=10)}' # Noncompliant: Move this assignment out of the f-string; ":=" operator is confusing in this context

Compliant solution

v0 = v1 = f(p)
x = 10
f'{x}'

Resources

Documentation