Skip to content

Commit f8650d4

Browse files
authored
Merge pull request #1382 from dbcli/RW/enable-bugbear-rule-B904-raise-exceptions-within-except-with-from
Enable lint rule B904: raise within `except` using `from`
2 parents a144971 + 72c23ff commit f8650d4

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

mycli/packages/special/iocommands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def set_tee(arg: str, **_) -> list[tuple]:
380380
try:
381381
tee_file = open(*parseargfile(arg))
382382
except (IOError, OSError) as e:
383-
raise OSError(f"Cannot write to file '{e.filename}': {e.strerror}")
383+
raise OSError(f"Cannot write to file '{e.filename}': {e.strerror}") from e
384384

385385
return [(None, None, None, "")]
386386

@@ -413,7 +413,7 @@ def set_once(arg: str, **_) -> list[tuple]:
413413
try:
414414
once_file = open(*parseargfile(arg))
415415
except (IOError, OSError) as e:
416-
raise OSError(f"Cannot write to file '{e.filename}': {e.strerror}")
416+
raise OSError(f"Cannot write to file '{e.filename}': {e.strerror}") from e
417417
written_to_once_file = False
418418

419419
return [(None, None, None, "")]
@@ -456,7 +456,7 @@ def _run_post_redirect_hook(post_redirect_command: str, filename: str) -> None:
456456
stderr=subprocess.DEVNULL,
457457
)
458458
except Exception as e:
459-
raise OSError(f"Redirect post hook failed: {e}")
459+
raise OSError(f"Redirect post hook failed: {e}") from e
460460

461461

462462
@special_command("\\pipe_once", "\\| command", "Send next result to a subprocess.", aliases=["\\|"])

mycli/packages/special/llm.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ def run_external_cmd(cmd, *args, capture_output=False, restart_cli=False, raise_
4242
code = e.code
4343
if code != 0 and raise_exception:
4444
if capture_output:
45-
raise RuntimeError(buffer.getvalue())
46-
else:
47-
raise RuntimeError(f"Command {cmd} failed with exit code {code}.")
45+
raise RuntimeError(buffer.getvalue()) from e
46+
raise RuntimeError(f"Command {cmd} failed with exit code {code}.") from e
4847
except Exception as e:
4948
code = 1
5049
if raise_exception:
5150
if capture_output:
52-
raise RuntimeError(buffer.getvalue())
53-
else:
54-
raise RuntimeError(f"Command {cmd} failed: {e}")
51+
raise RuntimeError(buffer.getvalue()) from e
52+
raise RuntimeError(f"Command {cmd} failed: {e}") from e
5553
if restart_cli and code == 0:
5654
os.execv(original_exe, [original_exe] + original_args)
5755
if capture_output:
@@ -211,7 +209,7 @@ def handle_llm(text, cur) -> Tuple[str, Optional[str], float]:
211209
context = ""
212210
return (context, sql, end - start)
213211
except Exception as e:
214-
raise RuntimeError(e)
212+
raise RuntimeError(e) from e
215213

216214

217215
def is_llm_command(command) -> bool:

mycli/packages/special/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ def execute(cur: Cursor, sql: str) -> list[tuple]:
119119

120120
try:
121121
special_cmd = COMMANDS[command]
122-
except KeyError:
122+
except KeyError as exc:
123123
special_cmd = COMMANDS[command.lower()]
124124
if special_cmd.case_sensitive:
125-
raise CommandNotFound(f'Command not found: {command}')
125+
raise CommandNotFound(f'Command not found: {command}') from exc
126126

127127
# "help <SQL KEYWORD> is a special case. We want built-in help, not
128128
# mycli help here.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ ignore = [
6767
'B005', # Multi-character strip()
6868
'B006', # TODO: Mutable data structures for argument defaults
6969
'B015', # TODO: Pointless comparison
70-
'B904', # TODO: Raise exceptions with "raise ... from err"
7170
'E401', # Multiple imports on one line
7271
'E402', # Module level import not at top of file
7372
'PIE808', # range() starting with 0

0 commit comments

Comments
 (0)