Skip to content
Open
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">=3.10"
dependencies = ["pytest>=7", "black>=23", "ruff>=0.5.0"]
dependencies = ["pytest>=7", "black>=23", "ruff>=0.12.9"]

[project.entry-points.pytest11]
examples = "pytest_examples"
Expand Down
8 changes: 5 additions & 3 deletions pytest_examples/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ def ruff_check(
if p.returncode == 1 and stdout:

def replace_offset(m: re.Match[str]):
line_number = int(m.group(1))
return f'{example.path}:{line_number + example.start_line}'
line_number = int(m.group(2))
return f'{m.group(1)}--> {example.path}:{line_number + example.start_line}'

output = re.sub(r'^-:(\d+)', replace_offset, stdout, flags=re.M)
# ruff >=0.12.9 renders the location on a separate ` --> file:line:col` line, indented to
# align with the source gutter, so the leading whitespace widens for multi-digit line numbers.
output = re.sub(r'^( *)--> -:(\d+)', replace_offset, stdout, flags=re.M)
raise FormatError(f'ruff failed:\n{indent(output, " ")}')
elif p.returncode != 0:
raise RuntimeError(f'Error running ruff, return code {p.returncode}:\n{stderr or stdout}')
Expand Down
17 changes: 15 additions & 2 deletions tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ def test_ruff_config():
def test_ruff_offset():
code = 'print(x)\n'
example = CodeExample.create(code)
with pytest.raises(FormatError, match='testing.md:1:7: F821 Undefined name'):
with pytest.raises(FormatError, match=r'F821 Undefined name `x`\n +--> testing.md:1:7'):
ruff_check(example, ExamplesConfig())

example = CodeExample.create(code, start_line=10)
with pytest.raises(FormatError, match='testing.md:11:7: F821 Undefined name'):
with pytest.raises(FormatError, match=r'F821 Undefined name `x`\n +--> testing.md:11:7'):
ruff_check(example, ExamplesConfig())


def test_ruff_offset_multiline():
# the error is on line 10 of the example, so ruff widens the gutter and indents `-->` further;
# the offset substitution must still rewrite the `-` placeholder to the real path
code = 'x = 1\n' * 9 + 'print(y)\n'
example = CodeExample.create(code)
with pytest.raises(FormatError, match=r'F821 Undefined name `y`\n +--> testing.md:10:7'):
ruff_check(example, ExamplesConfig())

example = CodeExample.create(code, start_line=10)
with pytest.raises(FormatError, match=r'F821 Undefined name `y`\n +--> testing.md:20:7'):
ruff_check(example, ExamplesConfig())


Expand Down
4 changes: 2 additions & 2 deletions tests/test_run_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def test_find_run_examples(example: CodeExample, eval_example: EvalExample):
'=== FAILURES ===\n',
'___ test_find_run_examples[my_file.md:1-4] ___\n',
'ruff failed:\n',
' my_file.md:2:8: F401 [*] `sys` imported but unused\n',
' my_file.md:3:7: F821 Undefined name `missing`\n',
' F401 [*] `sys` imported but unused\n --> my_file.md:2:8\n',
' F821 Undefined name `missing`\n --> my_file.md:3:7\n',
' Found 2 errors.\n',
' [*] 1 fixable with the `--fix` option.\n',
'=== short test summary info ===\n',
Expand Down
40 changes: 20 additions & 20 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading