diff --git a/src/colablinter/command.py b/src/colablinter/command.py index a10f4d4..ad2c1e4 100644 --- a/src/colablinter/command.py +++ b/src/colablinter/command.py @@ -2,20 +2,49 @@ from colablinter.logger import logger -CELL_CHECK_COMMAND = ( - "ruff check --select B,E,F,I,UP,SIM --ignore F401,E501 --stdin-filename=tmp.py" -) -CELL_CHECK_FIX_COMMAND = "ruff check --fix --select B,E,F,I,UP,SIM --ignore F401,E501 --stdin-filename=tmp.py" -CELL_CHECK_UNSAFE_FIX_COMMAND = "ruff check --fix --select B,E,F,I,UP,SIM --ignore F401,E501 --stdin-filename=tmp.py --unsafe-fixes" -CELL_FORMAT_COMMAND = "ruff format --stdin-filename=tmp.py" +CELL_CHECK_COMMAND = [ + "ruff", + "check", + "--select", + "B,E,F,I,UP,SIM", + "--ignore", + "F401,E501", + "--stdin-filename=tmp.py", +] +CELL_CHECK_FIX_COMMAND = [ + "ruff", + "check", + "--fix", + "--select", + "B,E,F,I,UP,SIM", + "--ignore", + "F401,E501", + "--stdin-filename=tmp.py", +] +CELL_CHECK_UNSAFE_FIX_COMMAND = [ + "ruff", + "check", + "--fix", + "--unsafe-fixes", + "--select", + "B,E,F,I,UP,SIM", + "--ignore", + "F401,E501", + "--stdin-filename=tmp.py", +] +CELL_FORMAT_COMMAND = [ + "ruff", + "format", + "--stdin-filename=tmp.py", +] -def execute_command(command: str, input_data: str) -> str | None: +def execute_command(command: list[str], input_data: str) -> str | None: try: result = subprocess.run( command, input=input_data, - shell=True, + shell=False, capture_output=True, text=True, encoding="utf-8", diff --git a/src/colablinter/magics.py b/src/colablinter/magics.py index fa2f0d1..9874fb8 100644 --- a/src/colablinter/magics.py +++ b/src/colablinter/magics.py @@ -29,7 +29,7 @@ def clcheck(self, line: str, cell: str) -> None: @cell_magic def clunsafefix(self, line: str, cell: str) -> None: if self.shell is None: - raise Exception + raise RuntimeError("IPython shell is not initialized.") stripped_cell = cell.strip() fixed_code = cell_check_unsafe_fix(stripped_cell) @@ -54,6 +54,9 @@ def clautofix(self, line: str) -> None: logger.info("Usage: %clautofix on or %clautofix off.") def __execute(self, cell: str) -> None: + if self.shell is None: + raise RuntimeError("IPython shell is not initialized.") + if self._is_autofix_active: logger.info( "autofix is temporarily suppressed to prevent dual execution. " @@ -62,8 +65,6 @@ def __execute(self, cell: str) -> None: self.__unregister() try: - if self.shell is None: - raise Exception self.shell.run_cell(cell, silent=False, store_history=True) except Exception as e: logger.exception(f"Code execution failed: {e}")