diff --git a/pyproject.toml b/pyproject.toml index 01a6f5f..cc750cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "colablinter" -version = "0.4.0" +version = "0.4.1" description = "Linting and formatting Python code in Google Colab." readme = "README.md" authors = [ diff --git a/src/colablinter/magics.py b/src/colablinter/magics.py index 9874fb8..b6908a3 100644 --- a/src/colablinter/magics.py +++ b/src/colablinter/magics.py @@ -18,16 +18,22 @@ class ColabLinterMagics(Magics): def __init__(self, **kwargs): super().__init__(**kwargs) self._is_autofix_active = True + self._is_processing = False self.timer = CellTimer() @cell_magic def clcheck(self, line: str, cell: str) -> None: + if self._is_processing: + return None stripped_cell = cell.strip() cell_check(stripped_cell) self.__execute(stripped_cell) @cell_magic def clunsafefix(self, line: str, cell: str) -> None: + if self._is_processing: + return None + if self.shell is None: raise RuntimeError("IPython shell is not initialized.") @@ -54,6 +60,9 @@ def clautofix(self, line: str) -> None: logger.info("Usage: %clautofix on or %clautofix off.") def __execute(self, cell: str) -> None: + if self._is_processing: + return None + if self.shell is None: raise RuntimeError("IPython shell is not initialized.") @@ -64,15 +73,20 @@ def __execute(self, cell: str) -> None: ) self.__unregister() + self._is_processing = True try: - self.shell.run_cell(cell, silent=False, store_history=True) + self.shell.run_cell(cell, silent=False, store_history=False) except Exception as e: logger.exception(f"Code execution failed: {e}") finally: + self._is_processing = False if self._is_autofix_active: self.__register() def __autofix(self, info: ExecutionInfo) -> None: + if self._is_processing or not self._is_autofix_active: + return None + if ( self.shell is None or info.raw_cell is None @@ -84,22 +98,23 @@ def __autofix(self, info: ExecutionInfo) -> None: if stripped_cell.startswith(("%", "!")) or stripped_cell == "": return None - formatted_code = cell_format(stripped_cell) - if formatted_code is None: - logger.error("Formatter failed during auto-format.") - return None - - fixed_code = cell_check_fix(formatted_code) + fixed_code = cell_check_fix(stripped_cell) if fixed_code is None: logger.error("Linter check failed during auto-format.") return None - self.shell.set_next_input(fixed_code, replace=True) + formatted_code = cell_format(fixed_code) + if formatted_code is None: + logger.error("Formatter failed during auto-format.") + return None + + self.shell.set_next_input(formatted_code, replace=True) def __register(self) -> None: if self.shell is None: - return None + raise RuntimeError("IPython shell is not initialized.") + self.__unregister() for event, callback in [ ("pre_run_cell", self.__autofix), ("pre_run_cell", self.timer.start), @@ -110,8 +125,9 @@ def __register(self) -> None: def __unregister(self) -> None: if self.shell is None: - return None + raise RuntimeError("IPython shell is not initialized.") + self.timer.start_time = None for event, callback in [ ("pre_run_cell", self.__autofix), ("pre_run_cell", self.timer.start), diff --git a/uv.lock b/uv.lock index 00ad4f8..ed186ab 100644 --- a/uv.lock +++ b/uv.lock @@ -17,7 +17,7 @@ wheels = [ [[package]] name = "colablinter" -version = "0.4.0" +version = "0.4.1" source = { virtual = "." } dependencies = [ { name = "ipython", version = "8.37.0", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },