Skip to main content

Replace Comparisons to Empty Sequence with Implicit Boolean Comparison

pixee:python/fix-empty-sequence-comparison

ImportanceReview GuidanceRequires Scanning Tool
LowMerge After ReviewNo

Empty sequences in Python always evaluate to False. This means that comparison expressions that use empty sequences can sometimes be simplified. In these cases no explicit comparison is required: instead we can rely on the truth value of the object under comparison. This is sometimes referred to as "implicit" comparison. Using implicit boolean comparison expressions is considered best practice and can lead to better code.

Our changes look like the following:

 x = [1]

- if x != []:
+ if x:
pass

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

F.A.Q.

Why is this codemod marked as Merge After Review?

Values compared to empty sequences should be verified in case they are falsy values that are not a sequence.

Codemod Settings

N/A

References