Why this matters
Prevents masking real failures and supports targeted recovery.
Catch and handle only the exceptions you expect (e.g., KeyError, ValueError, TimeoutError); let unknown ones bubble.
Prevents masking real failures and supports targeted recovery.
Side-by-side examples engineers can pattern-match during review.
try:
return cache[data]
except Exception:
return Nonetry:
return cache[data]
except KeyError:
logger.info('miss', extra={'key': data})
return Noneexcept Exception: passexcept (KeyError, ValueError) as e: ...From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.