From 577acf6b63edc8cf8ad89e93eafe903324fc0cfd Mon Sep 17 00:00:00 2001 From: David Montague <35119617+dmontagu@users.noreply.github.com> Date: Tue, 11 Jul 2023 11:01:12 -0600 Subject: [PATCH] Reenable docstring trimming in a way that works properly with python file examples --- pytest_examples/eval_example.py | 4 ++-- pytest_examples/lint.py | 6 ++++-- tests/cases_update/python_class.py | 2 -- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pytest_examples/eval_example.py b/pytest_examples/eval_example.py index 068fc70..aebe106 100644 --- a/pytest_examples/eval_example.py +++ b/pytest_examples/eval_example.py @@ -206,8 +206,8 @@ def format(self, example: CodeExample) -> None: :param example: The example to format. """ - self.format_black(example) self.format_ruff(example) + self.format_black(example) def format_black(self, example: CodeExample) -> None: """ @@ -217,7 +217,7 @@ def format_black(self, example: CodeExample) -> None: """ self._check_update(example) - new_content = black_format(example.source, self.config) + 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) diff --git a/pytest_examples/lint.py b/pytest_examples/lint.py index 3df91ae..4fe8f8c 100644 --- a/pytest_examples/lint.py +++ b/pytest_examples/lint.py @@ -65,17 +65,19 @@ def replace_offset(m: re.Match): return stdout -def black_format(source: str, config: ExamplesConfig) -> str: +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) + 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) raise FormatError(f'black failed:\n{indent(diff, " ")}') diff --git a/tests/cases_update/python_class.py b/tests/cases_update/python_class.py index 8288664..76a16c1 100644 --- a/tests/cases_update/python_class.py +++ b/tests/cases_update/python_class.py @@ -16,11 +16,9 @@ def foobar(): ```py x = 4 - class A: pass - print(x) #> 4 ```