Skip to main content

SKY-L005 · Logic and resilience

Unused exception variable

Low

The catalog gives no severity for this rule; the detector always reports LOW.

except SomeError as exc: binds the exception to a name. When the handler never reads that name, the binding does nothing.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans). This is advice that the default report does not list ("measurement"), and unlisted advice is not counted. --all-advice, or naming the rule in [tool.skylos] select or [tool.skylos.gate] rules, lists and counts it.
Finding type
advice
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

It is usually a sign that something was meant to happen with the error and did not: it was not logged, not chained with from exc, not shown to anyone. On its own the extra name is harmless.

What Skylos flags

app/config.py · PythonReported on line 8
import json  def read_config(path):    try:        with open(path, encoding="utf-8") as handle:            return json.load(handle)    except FileNotFoundError as exc:        return {}

What Skylos leaves alone

app/config.py · Python
import json  class ConfigError(Exception):    pass  def read_config(path):    try:        with open(path, encoding="utf-8") as handle:            return json.load(handle)    except json.JSONDecodeError as exc:        raise ConfigError(f"{path} is not valid JSON") from exc

Why Skylos leaves it alone

from exc reads the name, and chains the original error to the new one so its traceback is kept.

How Skylos decides

  1. Runs with --quality on Python files. Visits every except clause that binds a name with as; the name _ is skipped.
  2. Looks for any read of the name inside the handler, nested functions and lambdas included, or an augmented assignment such as exc += ....
  3. Also accepts a lookup by name through the namespace builtins: locals()["exc"], vars().get("exc") or "{exc}".format_map(locals()). That proof is dropped when the module rebinds locals or vars, uses a star import, or calls exec, eval or globals.
  4. Reports LOW on the except line when no read is found. The rule is advice: the finding is in JSON output, but the terminal list shows it only with --all-advice.

How to fix it

Drop as exc when the error is deliberately ignored, or use the name: log it, or chain it with raise ... from exc.

app/config.py · Python · the fix, against the flagged file
import json  def read_config(path):    try:        with open(path, encoding="utf-8") as handle:            return json.load(handle)Removed:     except FileNotFoundError as exc:Added:     except FileNotFoundError:        return {}

When it can be wrong

  • A name kept so a debugger can inspect the error is reported; there is no exemption for that.
  • Reads through other dynamic means, such as sys.exc_info() or a template that receives locals() without naming the key, are not seen as uses.

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 FileNotFoundError as exc:  # skylos: ignore[SKY-L005] kept for pdb
  • [tool.skylos] ignore = ["SKY-L005"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.