Skip to main content

Use Assignment Expression (Walrus) In Conditional

pixee:python/use-walrus-if

ImportanceReview GuidanceRequires Scanning Tool
LowMerge After Cursory ReviewNo

This codemod updates places where two separate statements involving an assignment and conditional can be replaced with a single Assignment Expression (commonly known as the walrus operator).

Many developers use this operator in new code that they write but don't have the time to find and update every place in existing code. So we do it for you! We believe this leads to more concise and readable code.

The changes from this codemod look like this:

- x = foo()
- if x is not None:
+ if (x := foo()) is not None:
print(x)

If you have feedback on this codemod, please let us know!

F.A.Q.

Why is this codemod marked as Merge After Cursory Review?

We believe that using the walrus operator is an improvement in terms of clarity and readability. However, this change is only compatible with codebases that support Python 3.8 and later, so it requires quick validation before merging.

Codemod Settings

N/A

References