colablinter is an IPython magic command extension designed for Jupyter and Google Colab notebooks.
It integrates the high-speed linter ruff to perform code quality checks and formatting directly within Jupyter/Colab cells.
It allows developers to lint code on a cell-by-cell basis with simple commands. Try it here:
| Command | Description |
|---|---|
%clautofix |
Activates or deactivates automatic fix including import sorting, formatting, and execution time display before every cell. |
%%clcheck |
Displays a linting report for the current cell. |
%%clunsafefix |
Performs a linting check and applies "unsafe" fixes that might change code logic (e.g., changing mutable default arguments). |
After executing a cell magic command, the checked/formatted code is immediately executed (if applicable), maintaining the notebook workflow.
Just run it in colab:
!uv pip install colablinterThe extension must be explicitly loaded in the notebook session before use.
%load_ext colablinterOnce loaded, %clautofix is activated by default to keep your code clean from the start.
-
Activate/Deactivate Auto Fix (
%clautofix)The
%clautofixline magic allows you to automatically fix code before every code cell is executed.import math, sys; class Example( object ): def __init__ ( self, bar ): if bar : bar+=1; bar=bar* bar ; return bar else: some_string = "foo" return (sys.path, some_string)
Fixed Cell:
import math import sys class Example: def __init__(self, bar): if bar: bar += 1 bar = bar * bar return bar else: some_string = "foo" return (sys.path, some_string)
To deactivate auto fixing:
%clautofix off # %clautofix on when you want to activate
-
Check cell quality (
%%clcheck)Use
%%clcheckto see linting reports for the code below the command. After the report is displayed, the code in the cell executes as normal.%%clcheck def invalid_code(x): return x + y
Output:
[ColabLinter:INFO] F821 Undefined name `y` --> tmp.py:2:16 | 1 | def invalid_code(x): 2 | return x + y | ^ | Found 1 error.
-
Unsafe Fix (
%%clunsafefix)Use
%%clunsafefixwhen you wantruffto aggressively clean up your code, including rules that are typically considered risky (like fixing mutable default arguments or complex refactoring).%%clunsafefix def process_data(new_item, current_list=[]): old_item = "redundant" if new_item: if isinstance(new_item, str): current_list.append(new_item) if len(current_list) > 0: return True else: return False
Fixed Cell:
def process_data(new_item, current_list=None): if current_list is None: current_list = [] if new_item and isinstance(new_item, str): current_list.append(new_item) return len(current_list) > 0
Note on F401: The linter is explicitly configured to ignore F401 errors (unused imports). This is to ensure compatibility with the stateful nature of Jupyter/Colab notebooks, where imports in one cell may be necessary for code execution in subsequent cells, preventing unintended breakage of the notebook's execution flow.
Magic Command Execution: When using magic or terminal commands while %clautofix is active, the auto-format mechanism is temporarily suppressed during the final execution step to prevent infinite loops or dual checks. If you want to disable auto-formatting, use %clautofix off