Skip to content

Pull Request Title: Update code formatting to use ruff format instead of black #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
28 changes: 1 addition & 27 deletions pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
27 changes: 5 additions & 22 deletions pytest_examples/lint.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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

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):
Expand Down Expand Up @@ -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)}'

Expand Down
Loading