python/dual-key-fallback
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| Python | warning | none, edit by hand | All versions |
Chaining .get() or getattr() across two different keys on the same object keeps a legacy field alias alive inside application logic, and or silently discards legitimate falsy values such as 0, False, or "". Normalize external input once at the boundary and read one canonical name everywhere else.
def customer_id(payload: dict[str, str]) -> str | None: return payload.get("customer_id") or payload.get("cust_id")def customer_id(payload: dict[str, str]) -> str | None: return payload.get("customer_id")What the rule reports
Section titled “What the rule reports”The rule reports three forms of dual-key compatibility fallback on the same object:
obj.get("new_key") or obj.get("old_key")(and chains starting withobj.get("new_key", None)).- Nested
getattr(obj, "new_attr", getattr(obj, "old_attr", ...))with two different attribute names on the same object. getattr(obj, "new_attr", None) or getattr(obj, "old_attr", ...)with two different attribute names on the same object.
The rule leaves alone fallbacks across two different objects, such as overrides.get("host") or defaults.get("host"), which layer configuration rather than aliasing a renamed key.
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Translate any external wire or storage field name once at the outermost ingestion boundary into a typed record with one canonical field name, and read only that canonical name throughout the rest of the codebase.
Diagnostic message
Section titled “Diagnostic message”Chaining $OBJ.get($KEY1) or $OBJ.get($KEY2) keeps a legacy key alias alive and silently skips falsy values such as 0 or "". Normalize keys once at the boundary and read one canonical key.See all rules in the Python pack.