Skip to content
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

Split ruff backend into separate backends for formatting and linting #20545

Merged
merged 28 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
230916d
Add experimental.python.fmt.ruff backend
krishnan-chandra Feb 14, 2024
76864fb
Move formatting rules into a separate file
krishnan-chandra Feb 14, 2024
713cffb
Fix broken import
krishnan-chandra Feb 14, 2024
2ab2c81
Allow dependency
krishnan-chandra Feb 14, 2024
93f541b
Fix imports in test
krishnan-chandra Feb 14, 2024
e2efc42
Move location of backend from fmt to lint
krishnan-chandra Feb 14, 2024
39e3404
Cleanup imports
krishnan-chandra Feb 14, 2024
26045b1
Fix copyright notices
krishnan-chandra Feb 14, 2024
a6af630
Update docs to add new backend
krishnan-chandra Feb 14, 2024
a1b4bfb
Update docs/docs/using-pants/key-concepts/backends.mdx
krishnan-chandra Feb 14, 2024
12a3427
Update docs/docs/using-pants/key-concepts/backends.mdx
krishnan-chandra Feb 14, 2024
6bd0d95
Update docs/docs/python/overview/linters-and-formatters.mdx
krishnan-chandra Feb 14, 2024
57de230
Update docs/docs/python/overview/linters-and-formatters.mdx
krishnan-chandra Feb 14, 2024
57be3f5
Update docs
krishnan-chandra Feb 15, 2024
b8dc294
Move ruff backends to lint/ruff/check and lint/ruff/format
krishnan-chandra Feb 15, 2024
a2e2897
Re-register check rules at root
krishnan-chandra Feb 15, 2024
51c3354
Add deprecation warning
krishnan-chandra Feb 15, 2024
739631a
Fix import ordering
krishnan-chandra Feb 15, 2024
05badd1
Add start and end versions
krishnan-chandra Feb 15, 2024
00209df
Bump versions by 1 more
krishnan-chandra Feb 15, 2024
dbe8016
Move deprecation to 2.20.0.dev0
krishnan-chandra Feb 15, 2024
57fcddb
Fix formatting of message
krishnan-chandra Feb 15, 2024
b3e5631
Use dev7 which is the latest release
krishnan-chandra Feb 15, 2024
16dde95
Update src/python/pants/backend/experimental/python/lint/ruff/registe…
krishnan-chandra Feb 15, 2024
d1d5901
Update src/python/pants/backend/experimental/python/lint/ruff/registe…
krishnan-chandra Feb 15, 2024
27783c9
Move rules.py -> check_rules.py
krishnan-chandra Feb 15, 2024
2a33da3
Fix import ordering
krishnan-chandra Feb 15, 2024
f210459
Update src/python/pants/backend/BUILD
krishnan-chandra Feb 15, 2024
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
27 changes: 14 additions & 13 deletions docs/docs/python/overview/linters-and-formatters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ Pants does several things to speed up running formatters and linters:

Linter/formatter support is implemented in separate [backends](../../using-pants/key-concepts/backends.mdx) so that they are easy to opt in to individually:

| Backend | Tool |
| :-------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| `pants.backend.python.lint.bandit` | [Bandit](https://bandit.readthedocs.io/en/latest/): security linter |
| `pants.backend.python.lint.black` | [Black](https://black.readthedocs.io/en/stable/): code formatter |
| `pants.backend.python.lint.docformatter` | [Docformatter](https://pypi.org/project/docformatter/): docstring formatter |
| `pants.backend.python.lint.flake8` | [Flake8](https://flake8.pycqa.org/en/latest/): style and bug linter |
| `pants.backend.python.lint.isort` | [isort](https://readthedocs.org/projects/isort/): import statement formatter |
| `pants.backend.python.lint.pydocstyle` | [Pydocstyle](https://pypi.org/project/pydocstyle/): docstring linter |
| `pants.backend.python.lint.pylint` | [Pylint](https://pylint.pycqa.org/): style and bug linter |
| `pants.backend.python.lint.yapf` | [Yapf](https://github.com/google/yapf): code formatter |
| `pants.backend.python.lint.autoflake` | [Autoflake](https://github.com/myint/autoflake): remove unused imports |
| `pants.backend.python.lint.pyupgrade` | [Pyupgrade](https://github.com/asottile/pyupgrade): automatically update code to use modern Python idioms like `f-strings` |
| `pants.backend.experimental.python.lint.ruff` | [Ruff](https://beta.ruff.rs/docs/): an extremely fast Python linter, written in Rust. |
| Backend | Tool |
| :--------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- |
| `pants.backend.python.lint.bandit` | [Bandit](https://bandit.readthedocs.io/en/latest/): security linter |
| `pants.backend.python.lint.black` | [Black](https://black.readthedocs.io/en/stable/): code formatter |
| `pants.backend.python.lint.docformatter` | [Docformatter](https://pypi.org/project/docformatter/): docstring formatter |
| `pants.backend.python.lint.flake8` | [Flake8](https://flake8.pycqa.org/en/latest/): style and bug linter |
| `pants.backend.python.lint.isort` | [isort](https://readthedocs.org/projects/isort/): import statement formatter |
| `pants.backend.python.lint.pydocstyle` | [Pydocstyle](https://pypi.org/project/pydocstyle/): docstring linter |
| `pants.backend.python.lint.pylint` | [Pylint](https://pylint.pycqa.org/): style and bug linter |
| `pants.backend.python.lint.yapf` | [Yapf](https://github.com/google/yapf): code formatter |
| `pants.backend.python.lint.autoflake` | [Autoflake](https://github.com/myint/autoflake): remove unused imports |
| `pants.backend.python.lint.pyupgrade` | [Pyupgrade](https://github.com/asottile/pyupgrade): automatically update code to use modern Python idioms like `f-strings` |
| `pants.backend.experimental.python.lint.ruff.check` | [Ruff (for linting)](https://docs.astral.sh/ruff/linter/): an extremely fast Python linter, written in Rust. |
| `pants.backend.experimental.python.lint.ruff.format` | [Ruff (for formatting)](https://docs.astral.sh/ruff/formatter/): an extremely fast Python code formatter, written in Rust. |

To enable, add the appropriate backends in `pants.toml`:

Expand Down
3 changes: 2 additions & 1 deletion docs/docs/using-pants/key-concepts/backends.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ The list of all backends (both stable and experimental) is also available via `p
| `pants.backend.experimental.python.framework.django` | Enables better support for projects using Django: [https://www.djangoproject.com](https://www.djangoproject.com) | |
| `pants.backend.experimental.python.framework.stevedore` | Enables better support for projects using stevedore: [https://docs.openstack.org/stevedore/](https://docs.openstack.org/stevedore/) | |
| `pants.backend.experimental.python.lint.add_trailing_comma` | Enables add-trailing-comma, a Python code formatter: [https://github.com/asottile/add-trailing-comma](https://github.com/asottile/add-trailing-comma) | [`add-trailing-comma`](../../../reference/subsystems/add-trailing-comma.mdx) |
| `pants.backend.experimental.python.lint.ruff` | Enables Ruff, an extremely fast Python linter: [https://beta.ruff.rs/docs/](https://beta.ruff.rs/docs/) | [Linters and formatters](../../python/overview/linters-and-formatters.mdx) |
| `pants.backend.experimental.python.lint.ruff.check` | Enables Ruff (for `lint`), an extremely fast Python linter: [https://docs.astral.sh/ruff/linter/](https://docs.astral.sh/ruff/linter/) | [Linters and formatters](../../python/overview/linters-and-formatters.mdx) |
| `pants.backend.experimental.python.lint.ruff.format` | Enables Ruff (for `fmt`), an extremely fast Python code formatter: [https://docs.astral.sh/ruff/formatter/](https://docs.astral.sh/ruff/formatter/) | [Linters and formatters](../../python/overview/linters-and-formatters.mdx) |
| `pants.backend.experimental.python.packaging.pyoxidizer` | Enables `pyoxidizer_binary` target. | [PyOxidizer](../../python/integrations/pyoxidizer.mdx) |
| `pants.backend.experimental.python.typecheck.pyright` | Enables Pyright, a Python type checker: [https://github.com/microsoft/pyright](https://github.com/microsoft/pyright) | |
| `pants.backend.experimental.python.typecheck.pytype` | Enables Pytype, a Python type checker: [https://google.github.io/pytype/](https://google.github.io/pytype/) | |
Expand Down
1 change: 1 addition & 0 deletions src/python/pants/backend/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ __dependents_rules__(
"[/build_files/fmt/yapf/register.py]",
"[/python/lint/black/rules.py]",
"[/python/lint/black/subsystem.py]",
"[/python/lint/ruff/fmt_rules.py]",
"[/python/lint/ruff/rules.py]",
"[/python/lint/ruff/subsystem.py]",
"[/python/lint/yapf/rules.py]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pants.backend.build_files.fmt.ruff.register import RuffRequest
from pants.backend.build_files.fmt.ruff.register import rules as ruff_build_rules
from pants.backend.python.lint.ruff.rules import rules as ruff_fmt_rules
from pants.backend.python.lint.ruff.check_rules import rules as ruff_fmt_rules
from pants.backend.python.lint.ruff.subsystem import Ruff
from pants.backend.python.lint.ruff.subsystem import rules as ruff_subsystem_rules
from pants.backend.python.target_types import PythonSourcesGeneratorTarget
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/build_files/fmt/ruff/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pants.backend.build_files.fmt.base import FmtBuildFilesRequest
from pants.backend.python.lint.ruff import subsystem as ruff_subsystem
from pants.backend.python.lint.ruff.rules import _run_ruff_fmt
from pants.backend.python.lint.ruff.fmt_rules import _run_ruff_fmt
from pants.backend.python.lint.ruff.subsystem import Ruff
from pants.backend.python.subsystems.python_tool_base import get_lockfile_interpreter_constraints
from pants.core.goals.fmt import FmtResult
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

"""Linter & formatter for Python.

See https://www.pantsbuild.org/docs/python-linters-and-formatters and https://docs.astral.sh/ruff/
"""

from pants.backend.python.lint.ruff import check_rules as ruff_rules
from pants.backend.python.lint.ruff import skip_field, subsystem


def rules():
return (*ruff_rules.rules(), *skip_field.rules(), *subsystem.rules())
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

"""Linter & formatter for Python.
See https://www.pantsbuild.org/docs/python-linters-and-formatters and https://docs.astral.sh/ruff/
"""

from pants.backend.python.lint.ruff import fmt_rules as ruff_fmt_rules
from pants.backend.python.lint.ruff import skip_field, subsystem


def rules():
return (*ruff_fmt_rules.rules(), *skip_field.rules(), *subsystem.rules())
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
See https://www.pantsbuild.org/docs/python-linters-and-formatters and https://docs.astral.sh/ruff/
"""

from pants.backend.python.lint.ruff import rules as ruff_rules
from pants.backend.python.lint.ruff import skip_field, subsystem
from pants.backend.experimental.python.lint.ruff.check import register as ruff_check
from pants.base.deprecated import warn_or_error


def rules():
return (*ruff_rules.rules(), *skip_field.rules(), *subsystem.rules())
warn_or_error(
"2.23.0.dev0",
"The `pants.backend.experimental.python.lint.ruff` backend",
hint="Use `pants.backend.experimental.python.lint.ruff.check` instead.",
start_version="2.20.0.dev7",
)
return ruff_check.rules()
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.fix import FixResult, FixTargetsRequest
from pants.core.goals.fmt import AbstractFmtRequest, FmtResult, FmtTargetsRequest
from pants.core.goals.lint import LintResult, LintTargetsRequest
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.core.util_rules.partitions import PartitionerType
Expand Down Expand Up @@ -59,43 +58,13 @@ def tool_id(cls) -> str:
return RuffLintRequest.tool_id


class RuffFormatRequest(FmtTargetsRequest):
field_set_type = RuffFieldSet
tool_subsystem = Ruff
partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION

@classproperty
def tool_name(cls) -> str:
return "ruff format"

@classproperty
def tool_id(self) -> str:
return "ruff-format"


@dataclass(frozen=True)
class _RunRuffRequest:
snapshot: Snapshot
mode: RuffMode
interpreter_constraints: Optional[InterpreterConstraints] = None


# Note - this function is kept separate because it is invoked from update_build_files.py, but
# not as a rule.
async def _run_ruff_fmt(
request: AbstractFmtRequest.Batch,
ruff: Ruff,
interpreter_constraints: Optional[InterpreterConstraints] = None,
) -> FmtResult:
run_ruff_request = _RunRuffRequest(
snapshot=request.snapshot,
mode=RuffMode.FORMAT,
interpreter_constraints=interpreter_constraints,
)
result = await _run_ruff(run_ruff_request, ruff)
return await FmtResult.create(request, result)


async def _run_ruff(
request: _RunRuffRequest,
ruff: Ruff,
Expand Down Expand Up @@ -169,16 +138,10 @@ async def ruff_lint(request: RuffLintRequest.Batch[RuffFieldSet, Any], ruff: Ruf
return LintResult.create(request, result)


@rule(desc="Format with `ruff format`", level=LogLevel.DEBUG)
async def ruff_fmt(request: RuffFormatRequest.Batch, ruff: Ruff) -> FmtResult:
return await _run_ruff_fmt(request, ruff)


def rules():
return [
*collect_rules(),
*RuffFixRequest.rules(),
*RuffLintRequest.rules(),
*RuffFormatRequest.rules(),
*pex.rules(),
]
57 changes: 57 additions & 0 deletions src/python/pants/backend/python/lint/ruff/fmt_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from typing import Optional

from pants.backend.python.lint.ruff.check_rules import _run_ruff, _RunRuffRequest
from pants.backend.python.lint.ruff.subsystem import Ruff, RuffFieldSet, RuffMode
from pants.backend.python.util_rules import pex
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.core.goals.fmt import AbstractFmtRequest, FmtResult, FmtTargetsRequest
from pants.core.util_rules.partitions import PartitionerType
from pants.engine.rules import collect_rules, rule
from pants.util.logging import LogLevel
from pants.util.meta import classproperty


class RuffFormatRequest(FmtTargetsRequest):
field_set_type = RuffFieldSet
tool_subsystem = Ruff
partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION

@classproperty
def tool_name(cls) -> str:
return "ruff format"

@classproperty
def tool_id(self) -> str:
return "ruff-format"


# Note - this function is kept separate because it is invoked from update_build_files.py, but
# not as a rule.
async def _run_ruff_fmt(
request: AbstractFmtRequest.Batch,
ruff: Ruff,
interpreter_constraints: Optional[InterpreterConstraints] = None,
) -> FmtResult:
run_ruff_request = _RunRuffRequest(
snapshot=request.snapshot,
mode=RuffMode.FORMAT,
interpreter_constraints=interpreter_constraints,
)
result = await _run_ruff(run_ruff_request, ruff)
return await FmtResult.create(request, result)


@rule(desc="Format with `ruff format`", level=LogLevel.DEBUG)
async def ruff_fmt(request: RuffFormatRequest.Batch, ruff: Ruff) -> FmtResult:
return await _run_ruff_fmt(request, ruff)


def rules():
return [
*collect_rules(),
*RuffFormatRequest.rules(),
*pex.rules(),
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from pants.backend.python import target_types_rules
from pants.backend.python.lint.ruff import skip_field
from pants.backend.python.lint.ruff.rules import RuffFixRequest, RuffFormatRequest, RuffLintRequest
from pants.backend.python.lint.ruff.rules import rules as ruff_rules
from pants.backend.python.lint.ruff.check_rules import RuffFixRequest, RuffLintRequest
from pants.backend.python.lint.ruff.check_rules import rules as ruff_rules
from pants.backend.python.lint.ruff.fmt_rules import RuffFormatRequest
from pants.backend.python.lint.ruff.fmt_rules import rules as ruff_fmt_rules
from pants.backend.python.lint.ruff.subsystem import RuffFieldSet
from pants.backend.python.lint.ruff.subsystem import rules as ruff_subsystem_rules
from pants.backend.python.target_types import PythonSourcesGeneratorTarget
Expand All @@ -35,6 +37,7 @@ def rule_runner() -> RuleRunner:
return RuleRunner(
rules=[
*ruff_rules(),
*ruff_fmt_rules(),
*skip_field.rules(),
*ruff_subsystem_rules(),
*config_files.rules(),
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/core/goals/update_build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pants.backend.python.goals import lockfile
from pants.backend.python.lint.black.rules import _run_black
from pants.backend.python.lint.black.subsystem import Black
from pants.backend.python.lint.ruff.rules import _run_ruff_fmt
from pants.backend.python.lint.ruff.fmt_rules import _run_ruff_fmt
from pants.backend.python.lint.ruff.subsystem import Ruff
from pants.backend.python.lint.yapf.rules import _run_yapf
from pants.backend.python.lint.yapf.subsystem import Yapf
Expand Down
Loading