Skip to content

Commit

Permalink
add ruff_select, use --extend-select, add ignore_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Mar 26, 2023
1 parent a36b4ca commit 4c9173d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pytest_examples/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ExamplesConfig:
upgrade: bool = False
isort: bool = False
ruff_line_length: int | None = None
ruff_select: list[str] | None = None
ruff_ignore: list[str] | None = None

def black_mode(self):
Expand Down Expand Up @@ -54,6 +55,9 @@ def ruff_config(self) -> tuple[str, ...]:
else:
args.append(f'--line-length={self.ruff_line_length}')

if self.ruff_select:
select.extend(self.ruff_select)

if self.quotes == 'single':
# enforce single quotes using ruff, black will enforce double quotes
select.append('Q')
Expand Down
5 changes: 4 additions & 1 deletion pytest_examples/eval_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def set_config(
upgrade: bool = False,
isort: bool = False,
ruff_line_length: int | None = None,
ruff_select: list[str] | None = None,
ruff_ignore: list[str] | None = None,
):
"""
Expand All @@ -53,7 +54,8 @@ def set_config(
:param upgrade: If True, upgrade the code to the target version, defaults to False.
:param isort: If True, run ruff's isort extension on the code, defaults to False.
:param ruff_line_length: In general, we disable line-length checks in ruff, to let black take care of them.
:param ruff_ignore: Ruff rule to ignore
:param ruff_select: Ruff rules to select
:param ruff_ignore: Ruff rules to ignore
"""
self.config = ExamplesConfig(
line_length=line_length,
Expand All @@ -63,6 +65,7 @@ def set_config(
upgrade=upgrade,
isort=isort,
ruff_line_length=ruff_line_length,
ruff_select=ruff_select,
ruff_ignore=ruff_ignore,
)

Expand Down
8 changes: 5 additions & 3 deletions pytest_examples/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ class FormatError(ValueError):
def ruff_format(
example: CodeExample,
config: ExamplesConfig | None,
*,
ignore_errors: bool = False,
) -> str:
args = ('--fix',)
if ignore_errors:
args += ('--exit-zero',)
try:
return ruff_check(example, config, extra_ruff_args=args)
except FormatError:
Expand All @@ -43,9 +47,7 @@ def ruff_check(
*,
extra_ruff_args: tuple[str, ...] = (),
) -> str:
args = 'ruff', '-', *config.ruff_config()

args += extra_ruff_args
args = 'ruff', '-', *config.ruff_config(), *extra_ruff_args

p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stdout, stderr = p.communicate(example.source, timeout=2)
Expand Down

0 comments on commit 4c9173d

Please sign in to comment.