Skip to main content
ai-slop/python-isinstance-laddermanual fix

isinstance ladder.

Four or more `if`/`elif isinstance(x, ...)` branches testing the same variable. Type dispatch written out by hand, one branch per type.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

python-isinstance-ladder.py
✗ Flagged
def to_json(value):
    if isinstance(value, datetime):
        return value.isoformat()
    elif isinstance(value, Decimal):
        return str(value)
    elif isinstance(value, UUID):
        return str(value)
    elif isinstance(value, Enum):
        return value.value
    return value
Why this trips

Deterministic line match: counts `if`/`elif isinstance(name, ...)` branches on the same variable at the same indentation and flags the ladder at four or more.

python-isinstance-ladder.py
✓ Clean
@singledispatch
def to_json(value):
    return value

to_json.register(datetime, lambda value: value.isoformat())
to_json.register(Decimal, str)
to_json.register(UUID, str)
to_json.register(Enum, lambda value: value.value)
What changed

Use a handler map keyed by type, `functools.singledispatch`, or a protocol, or normalize inputs into one representation before the logic runs.

Asked to support one more input type, agents add one more `elif isinstance` branch. Across iterations the ladder grows instead of the design absorbing new cases.

How this rule is justified.

Rule id

ai-slop/python-isinstance-ladder

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: counts `if`/`elif isinstance(name, ...)` branches on the same variable at the same indentation and flags the ladder at four or more.

Legitimate code that is NOT flagged

Ladders of three or fewer branches, and branches that test different variables, are not flagged.

Evidence

Shipped in aislop v0.9.4 as one of four Python verbosity rules drawn from SlopCodeBench (arXiv 2603.24755).