From fe57a2bf007e884bc167971f630441bc56613e93 Mon Sep 17 00:00:00 2001 From: phanirithvij Date: Sat, 13 Sep 2025 20:44:38 +0530 Subject: [PATCH 1/2] ruff: adapt new file lookup Signed-off-by: phanirithvij --- nbqa/output_parser.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nbqa/output_parser.py b/nbqa/output_parser.py index fbadfe42..c81e597a 100644 --- a/nbqa/output_parser.py +++ b/nbqa/output_parser.py @@ -68,6 +68,15 @@ def _get_pattern( ), ] + if command.startswith("ruff"): + return [ + ( + rf"(?<=--> {re.escape(relative_path)}:)\d+" + rf"|(?<=--> {re.escape(absolute_path)}:)\d+", + standard_substitution, + ) + ] + # This is the most common one and is used by flake, pylint, mypy, and more. return [ ( From e715abb2d0ffc77314749b8c24d268256af4f072 Mon Sep 17 00:00:00 2001 From: phanirithvij Date: Sat, 13 Sep 2025 20:44:51 +0530 Subject: [PATCH 2/2] ruff: update tests for new file pattern Signed-off-by: phanirithvij --- tests/tools/test_ruff_works.py | 46 +++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/tools/test_ruff_works.py b/tests/tools/test_ruff_works.py index 659dc287..1028d4aa 100644 --- a/tests/tools/test_ruff_works.py +++ b/tests/tools/test_ruff_works.py @@ -53,26 +53,38 @@ def test_ruff_works( expected_path_2 = os.path.join("tests", "data", "notebook_starting_with_md.ipynb") out, err = capsys.readouterr() + # ignore ruff's suggestions - out = "\n".join([x for x in out.splitlines() if "cell_" in x]) + prev = "" + output: list[str] = [] + for line in out.splitlines(): + if "cell_" in line: + # append previous line and matching line + output.append(prev) + output.append(line) + prev = line + expected_out = ( - f"{expected_path_1}:cell_1:1:8: F401 [*] `os` imported but unused\n" - f"{expected_path_1}:cell_1:3:8: F401 [*] `glob` imported but unused\n" - f"{expected_path_1}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n" - f"{expected_path_0}:cell_1:1:8: F401 [*] `os` imported but unused\n" - f"{expected_path_0}:cell_1:3:8: F401 [*] `glob` imported but unused\n" - f"{expected_path_0}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n" - f"{expected_path_0}:cell_4:1:1: E402 Module level import not at top of file\n" - f"{expected_path_0}:cell_4:1:20: F401 [*] `random.randint` imported but unused\n" - f"{expected_path_0}:cell_5:1:1: E402 Module level import not at top of file\n" - f"{expected_path_0}:cell_5:2:1: E402 Module level import not at top of file\n" - f"{expected_path_2}:cell_1:1:8: F401 [*] `os` imported but unused\n" - f"{expected_path_2}:cell_1:3:8: F401 [*] `glob` imported but unused\n" - f"{expected_path_2}:cell_1:5:8: F401 [*] `nbqa` imported but unused\n" - ) - assert "\n".join(sorted(out.replace("\r\n", "\n").splitlines())) == "\n".join( - sorted(expected_out.splitlines()) + f"F401 [*] `os` imported but unused\n --> {expected_path_1}:cell_1:1:8\n" + f"F401 [*] `glob` imported but unused\n --> {expected_path_1}:cell_1:3:8\n" + f"F401 [*] `nbqa` imported but unused\n --> {expected_path_1}:cell_1:5:8\n" + f"F401 [*] `os` imported but unused\n --> {expected_path_0}:cell_1:1:8\n" + f"F401 [*] `glob` imported but unused\n --> {expected_path_0}:cell_1:3:8\n" + f"F401 [*] `nbqa` imported but unused\n --> {expected_path_0}:cell_1:5:8\n" + f"E402 Module level import not at top of file\n --> {expected_path_0}:cell_4:1:1\n" + f"F401 [*] `random.randint` imported but unused\n --> {expected_path_0}:cell_4:1:20\n" + f"E402 Module level import not at top of file\n --> {expected_path_0}:cell_5:1:1\n" + f"E402 Module level import not at top of file\n --> {expected_path_0}:cell_5:2:1\n" + f"F401 [*] `os` imported but unused\n --> {expected_path_2}:cell_1:1:8\n" + f"F401 [*] `glob` imported but unused\n --> {expected_path_2}:cell_1:3:8\n" + f"F401 [*] `nbqa` imported but unused\n --> {expected_path_2}:cell_1:5:8\n" ) + + # simple dedent of '\s+-->' + out = "\n".join(sorted([x.lstrip() for x in output])) + exp = "\n".join(sorted([x.lstrip() for x in expected_out.splitlines()])) + + assert out == exp assert err == ""