Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions nbqa/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def _get_pattern(
),
]

if command.startswith("ruff"):
return [
(
rf"(?<=--> {re.escape(relative_path)}:)\d+"
Copy link
Contributor Author

@phanirithvij phanirithvij Sep 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused what's required here.

I tried rf"(?<=\s+--> {re.escape(relative_path)}:)\d+" but it fails because re.PatternError: look-behind requires fixed-width pattern. so \s+ is not allowed.

I saw both _--> and __--> in the ruff tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your fix

I'm not sure what you mean here, sorry - did you figure out a fix?

rf"|(?<=--> {re.escape(absolute_path)}:)\d+",
standard_substitution,
)
]

# This is the most common one and is used by flake, pylint, mypy, and more.
return [
(
Expand Down
46 changes: 29 additions & 17 deletions tests/tools/test_ruff_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I am doing lstrip here to handle arbitrary indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my fix for the above issue is here.

exp = "\n".join(sorted([x.lstrip() for x in expected_out.splitlines()]))

assert out == exp
assert err == ""


Expand Down
Loading