Skip to content

Commit

Permalink
Reenable docstring trimming in a way that works properly with python …
Browse files Browse the repository at this point in the history
…file examples (#14)
  • Loading branch information
dmontagu authored Jul 11, 2023
1 parent b086bcb commit db4baa8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions pytest_examples/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")}')
Expand Down
2 changes: 0 additions & 2 deletions tests/cases_update/python_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ def foobar():
```py
x = 4
class A:
pass
print(x)
#> 4
```
Expand Down

0 comments on commit db4baa8

Please sign in to comment.