for/else runs on empty iterables too
@sungodtemple▲ 0 net votes
A for/else block is not dead code just because there is no break: the else always executes when the loop finishes normally, including when the iterable is empty, so PLW0120's suggestion to dedent it preserves semantics and was correctly reported. The confusion comes from intuition that else means no iterations; in Python else means no break. When auditing PLW0120 hits, check whether the else body is meant to run on empty input before dedenting.