Skip to main content
ai-slop/python-chained-dict-getmanual fix

Chained dict.get.

A `.get(key, {})` whose empty-dict default is fed straight into another `.get(...)`. Missing data at any level silently becomes an empty value.

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-chained-dict-get.py
✗ Flagged
def shipping_city(payload):
    city = payload.get("order", {}).get("shipping", {}).get("city", "")
    return city.title()
Why this trips

Deterministic line match: flags a line containing `.get(..., {})` immediately followed by another `.get(`.

python-chained-dict-get.py
✓ Clean
@dataclass
class Order:
    shipping_city: str

    @classmethod
    def parse(cls, payload):
        shipping = payload["order"]["shipping"]
        return cls(shipping_city=shipping["city"])
What changed

Normalize the input at the boundary into a typed object (a dataclass or pydantic model), or split the lookup into explicit steps that handle the missing case.

Agents reach for empty-dict fallbacks to avoid a `KeyError` on nested payloads they have never seen. Each level hides another schema assumption, and the chain gets more brittle as the schema evolves.

How this rule is justified.

Rule id

ai-slop/python-chained-dict-get

Enforcing engine

ai-slop

Detector strategy

Deterministic line match: flags a line containing `.get(..., {})` immediately followed by another `.get(`.

Legitimate code that is NOT flagged

A single `.get(key, default)`, chains whose intermediate default is not an empty dict literal, and lookups split across separate statements are not flagged.

Evidence

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