Skip to content

prefer-set-isdisjoint

Prefer set.isdisjoint when a built-in set intersection is used only as a boolean predicate.

Why

isdisjoint names the overlap predicate directly and avoids allocating an intersection that is immediately discarded.

Fix

Use left.isdisjoint(right) and negate it when the condition requires overlap.

Examples

Before — flagged Set intersection is used only for an emptiness test
app/policy.py
allowed = {"read", "write"}
requested = set(scopes)
if not (allowed & requested):
deny()
After — preferred Set overlap is expressed without allocating an intersection
app/policy.py
allowed = {"read", "write"}
requested = set(scopes)
if allowed.isdisjoint(requested):
deny()