Re-raise loses the cause
This is how the comment reads inline, under the flagged line.
Why it matters
Raising a new exception inside except without from buries the original error in 'During handling…' noise.
What to do
raise NewError(...) from err (or from None when hiding it is the point).
Examples
except KeyError:
raise ConfigError('missing key')except KeyError as err:
raise ConfigError('missing key') from errWhat it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
try:
go()
except KeyError:
raise ValueError('x')
Left alone
try:
go()
except KeyError as e:
raise ValueError('x') from e
Research
Bare `except:`, `except Exception`, and a re-raise that drops the cause. It catches bugs you did not expect along with the error you did, and loses the original traceback.
- Ruff: blind-except
- Ruff: bare-except
- skill: code-review
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"raise-without-from"
],
"severity": {
"raise-without-from": "medium"
}
}
}
exceptwithoutfrom. The original cause is hidden in the traceback.