Collection To Bool

Python pattern

Replace constant collection with boolean in boolean contexts.


Apply with the Grit CLI
grit apply collection_to_bool

Collection to bool

BEFORE
if ["foo", "boo"]:
    baz()
if ["foo"]:
    baz()
if []:
    baz()
if {}:
    baz()
if {1: 1, 2: 2, 3: 3}:
    baz()
if {1, 2, 3}:
    baz()
if (1, 2, 3):
    baz()
if ():
    baz()

if "foo":
    baz()
if [x for x in y]:
    baz()
AFTER
if True:
    baz()
if True:
    baz()
if False:
    baz()
if False:
    baz()
if True:
    baz()
if True:
    baz()
if True:
    baz()
if False:
    baz()

if "foo":
    baz()
if [x for x in y]:
    baz()