-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the ruff linter and formatter
This is a new linter that is gaining in popularity quickly, let's add a predefined step for it. This also now sets CLICOLOR_FORCE=1 when color is requested, as this is what ruff listens for, and is a standard for quite a few tools
- Loading branch information
1 parent
4579155
commit 23c06cf
Showing
13 changed files
with
177 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from typing import List, Optional, Sequence | ||
|
||
# XXX: All imports here should be done from the top level. If we need it, | ||
# users might need it | ||
from .. import Step, StepRunner, build_parameters, set_defaults | ||
|
||
|
||
@set_defaults( | ||
{ | ||
"dependencies": ["ruff"], | ||
"files": ["."], | ||
"additional_arguments": ["check"], | ||
} | ||
) | ||
class Ruff(Step): | ||
def __init__(self) -> None: | ||
self.__name__ = "ruff" | ||
|
||
def __call__( | ||
self, | ||
step: StepRunner, | ||
files: Sequence[str], | ||
additional_arguments: List[str], | ||
) -> None: | ||
step.run( | ||
["ruff", *additional_arguments, *files], | ||
env={"RUFF_CACHE_DIR": str(step.cache_path / "ruff-cache")}, | ||
) | ||
|
||
|
||
def ruff( | ||
*, | ||
files: Optional[Sequence[str]] = None, | ||
additional_arguments: Optional[List[str]] = None, | ||
) -> Step: | ||
""" | ||
Run `Ruff`_ against your python source code. | ||
By default, it will depend on :python:`["ruff"]`, when registered with | ||
:py:func:`dwas.register_managed_step`. | ||
:param files: The list of files or directories to run ``ruff`` against. | ||
Defaults to :python:`["."]`. | ||
:param additional_arguments: Additional arguments to pass to the ``ruff`` | ||
invocation. Defaults to :python:`["check"]`. | ||
Defaults to :python:`["--check", "--diff", "-W1"]`. | ||
:return: The step so that you can add additional parameters to it if needed. | ||
:Examples: | ||
In order to verify your code but not change it, for a step | ||
named **ruff**: | ||
.. code-block:: | ||
register_managed_step(dwas.predefined.ruff()) | ||
Or, in order to automatically fix your code, but only if requested: | ||
.. code-block:: | ||
register_managed_step( | ||
dwas.predefined.ruff(additional_arguments=["check", "--fix"]), | ||
# NOTE: this name is arbitrary, you could omit it, or specify | ||
# something else. We suffix in our documentation all | ||
# operations that will have destructive effect on the source | ||
# code by ``:fix`` | ||
name="ruff:fix", | ||
run_by_default=False, | ||
) | ||
Similarly, if you want to use ruff to format your code you could do: | ||
.. code-block:: | ||
# To check the formatting | ||
register_managed_step( | ||
dwas.predefined.ruff(additional_arguments=["format", "--diff"]), | ||
name="ruff:format-check", | ||
) | ||
# To autoformat | ||
register_managed_step( | ||
dwas.predefined.ruff(additional_arguments=["format"]), | ||
name="ruff:format", | ||
) | ||
""" | ||
return build_parameters( | ||
files=files, additional_arguments=additional_arguments | ||
)(Ruff()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
|
||
from .mixins import BaseLinterWithAutofixTest | ||
|
||
|
||
class TestRuffCheck(BaseLinterWithAutofixTest): | ||
dwasfile = """\ | ||
from dwas import register_managed_step | ||
from dwas.predefined import ruff | ||
register_managed_step(ruff()) | ||
register_managed_step( | ||
ruff(additional_arguments=["check", "--fix"]), | ||
name="ruff:fix", | ||
run_by_default=False, | ||
) | ||
""" | ||
invalid_file = """\ | ||
from pathlib import Path | ||
import os | ||
""" | ||
valid_file = '"""This is a token file"""\n' | ||
autofix_step = "ruff:fix" | ||
|
||
|
||
class TestRuffFormat(BaseLinterWithAutofixTest): | ||
dwasfile = """\ | ||
from dwas import register_managed_step | ||
from dwas.predefined import ruff | ||
register_managed_step(ruff(additional_arguments=["format", "--diff"])) | ||
register_managed_step( | ||
ruff(additional_arguments=["format"]), | ||
name="ruff:fix", | ||
run_by_default=False, | ||
) | ||
""" | ||
autofix_step = "ruff:fix" | ||
invalid_file = "x = 1" | ||
valid_file = "x = 1\n" | ||
|
||
@pytest.mark.skip("ruff format does not support colored output") | ||
def test_respects_color_settings(self): | ||
pass # pragma: nocover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters