SKY-L002 · Logic and resilience
Bare except block
An except: with no exception type catches everything, including KeyboardInterrupt and SystemExit. Those are not errors: they are how Ctrl+C and sys.exit() stop a program.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
A bare except: around a slow call swallows the stop request, so Ctrl+C or a worker shutdown is logged as a cache error and the program keeps running. It also hides real bugs, such as a NameError, behind the same fallback.
What Skylos flags
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__) def cached_rate(cache, currency): try: return cache.get(f"rate:{currency}") except: logger.warning("rate cache unavailable, falling back to the API") return NoneWhat Skylos leaves alone
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__) def cached_rate(cache, currency): try: return cache.get(f"rate:{currency}") except Exception: logger.warning("rate cache unavailable, falling back to the API") return NoneWhy Skylos leaves it alone
except Exception: is still broad, but it does not catch KeyboardInterrupt, SystemExit or GeneratorExit, so the program can still be stopped. SKY-L002 only looks for a handler with no type at all. A narrower type, as in the fix, is better still.
How Skylos decides
- Runs with
--qualityon Python files. Visits everyexceptclause, includingexcept*groups. - Reports MEDIUM when the clause names no exception type (
except:). Any named type, evenexcept BaseException:, is not reported by this rule. - Does not look at the handler body: a bare
except:that logs, cleans up or re-raises is reported the same way. The finding is on theexceptline.
How to fix it
Catch the errors the call can actually raise. Here the Redis client raises connection and timeout errors when the cache is down.
"""Read-through cache for exchange rates.""" import logging logger = logging.getLogger(__name__) def cached_rate(cache, currency): try: return cache.get(f"rate:{currency}")Removed: except:Added: except (ConnectionError, TimeoutError): logger.warning("rate cache unavailable, falling back to the API") return NoneWhen it can be wrong
- A bare
except:that ends withraisepasses every exception on, so it swallows nothing; it is still reported.except BaseException:withraisesays the same thing more clearly and is not reported. - A bare
except: passorexcept: return Noneis also reported by SKY-L007 (empty error handler). SKY-L030 does not report bare handlers; it coversexcept Exception:with a placeholder return.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Put the marker on the
except:line.Python except: # skylos: ignore[SKY-L002] re-raised below after cleanup# noqa: E722, the flake8 code for the same problem, is not read: Skylos ignoresnoqacomments that name codes. A bare# noqawith no code does hide the finding.[tool.skylos] ignore = ["SKY-L002"]turns the rule off for the project.