diff --git a/pytest_examples/eval_example.py b/pytest_examples/eval_example.py index ad5e5ca..b45213d 100644 --- a/pytest_examples/eval_example.py +++ b/pytest_examples/eval_example.py @@ -8,7 +8,7 @@ from _pytest.outcomes import Failed as PytestFailed from .config import DEFAULT_LINE_LENGTH, ExamplesConfig -from .lint import FormatError, black_check, black_format, ruff_check, ruff_format +from .lint import FormatError, ruff_check, ruff_format from .run_code import InsertPrintStatements, run_code if TYPE_CHECKING: @@ -182,20 +182,8 @@ def lint(self, example: CodeExample) -> None: Args: example: The example to lint. """ - self.lint_black(example) self.lint_ruff(example) - def lint_black(self, example: CodeExample) -> None: - """Lint the example using black. - - Args: - example: The example to lint. - """ - example.test_id = self._test_id - try: - black_check(example, self.config) - except FormatError as exc: - raise PytestFailed(str(exc), pytrace=False) from None def lint_ruff( self, @@ -219,20 +207,6 @@ def format(self, example: CodeExample) -> None: example: The example to format. """ self.format_ruff(example) - self.format_black(example) - - def format_black(self, example: CodeExample) -> None: - """Format the example using black, requires `--update-examples`. - - Args: - example: The example to lint. - """ - self._check_update(example) - - new_content = black_format(example.source, self.config, remove_double_blank=example.in_py_file()) - if new_content != example.source: - example.source = new_content - self._mark_for_update(example) def format_ruff( self, diff --git a/pytest_examples/lint.py b/pytest_examples/lint.py index cec01cb..6083020 100644 --- a/pytest_examples/lint.py +++ b/pytest_examples/lint.py @@ -1,12 +1,11 @@ from __future__ import annotations as _annotations import re +import difflib from subprocess import PIPE, Popen from textwrap import indent from typing import TYPE_CHECKING -from black import format_str as black_format_str -from black.output import diff as black_diff from ruff.__main__ import find_ruff_bin from .config import ExamplesConfig @@ -14,7 +13,7 @@ if TYPE_CHECKING: from .find_examples import CodeExample -__all__ = 'ruff_check', 'ruff_format', 'black_check', 'black_format', 'code_diff', 'FormatError' +__all__ = 'ruff_check', 'ruff_format', 'code_diff', 'FormatError' class FormatError(ValueError): @@ -67,28 +66,12 @@ def replace_offset(m: re.Match): return stdout -def black_format(source: str, config: ExamplesConfig, *, remove_double_blank: bool = False) -> str: - # hack to avoid black complaining about our print output format - before_black = re.sub(r'^( *#)> ', r'\1 > ', source, flags=re.M) - after_black = black_format_str(before_black, mode=config.black_mode()) - # then revert it back - after_black = re.sub(r'^( *#) > ', r'\1> ', after_black, flags=re.M) - if remove_double_blank: - after_black = re.sub(r'\n{3}', '\n\n', after_black) - return after_black - - -def black_check(example: CodeExample, config: ExamplesConfig) -> None: - after_black = black_format(example.source, config, remove_double_blank=example.in_py_file()) - if example.source != after_black: - diff = code_diff(example, after_black, config) - raise FormatError(f'black failed:\n{indent(diff, " ")}') def code_diff(example: CodeExample, after: str, config: ExamplesConfig) -> str: - diff = black_diff(sub_space(example.source, config), sub_space(after, config), 'before', 'after') - - def replace_at_line(match: re.Match) -> str: + before_lines = sub_space(example.source, config).splitlines(keepends=True) + after_lines = sub_space(after, config).splitlines(keepends=True) + diff = ''.join(difflib.unified_diff(before_lines, after_lines, 'before', 'after')) offset = re.sub(r'\d+', lambda m: str(int(m.group(0)) + example.start_line), match.group(2)) return f'{match.group(1)}{offset}{match.group(3)}'