Skip to content

Commit

Permalink
Split skip ruff field into two (#20626)
Browse files Browse the repository at this point in the history
Closes #20620.

Added tests for both new fields; I couldn't figure out how to get the
deprecated alias `skip_ruff` to still work in tests.

I had another version of this change which kept the `skip_ruff` behavior
as it is now to skip **both** linting and formatting; if that's a
preferred solution, I can tack on those changes to this branch.
  • Loading branch information
krishnan-chandra authored Feb 29, 2024
1 parent 1aed24f commit a67f30e
Show file tree
Hide file tree
Showing 17 changed files with 408 additions and 200 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/backend/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ __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/check_rules.py]",
"[/python/lint/ruff/format/rules.py]",
"[/python/lint/ruff/check/rules.py]",
"[/python/lint/ruff/subsystem.py]",
"[/python/lint/yapf/rules.py]",
"[/python/lint/yapf/subsystem.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.check_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
4 changes: 3 additions & 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,8 @@

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.fmt_rules import _run_ruff_fmt
from pants.backend.python.lint.ruff.format import rules as ruff_format_backend
from pants.backend.python.lint.ruff.format.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 All @@ -25,5 +26,6 @@ def rules():
return [
*collect_rules(),
*RuffRequest.rules(),
*ruff_format_backend.rules(),
*ruff_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 check_rules as ruff_rules
from pants.backend.python.lint.ruff import skip_field, subsystem
from pants.backend.python.lint.ruff.check import rules as ruff_check_backend
from pants.backend.python.lint.ruff.check import skip_field as ruff_check_skip_field


def rules():
return (*ruff_rules.rules(), *skip_field.rules(), *subsystem.rules())
return (
*skip_field.rules(),
*ruff_check_backend.rules(),
*ruff_check_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 fmt_rules as ruff_fmt_rules
from pants.backend.python.lint.ruff import skip_field, subsystem
from pants.backend.python.lint.ruff.format import rules as ruff_format_backend
from pants.backend.python.lint.ruff.format import skip_field as ruff_format_skip_field


def rules():
return (*ruff_fmt_rules.rules(), *skip_field.rules(), *subsystem.rules())
return (
*skip_field.rules(),
*ruff_format_backend.rules(),
*ruff_format_skip_field.rules(),
*subsystem.rules(),
)
4 changes: 4 additions & 0 deletions src/python/pants/backend/python/lint/ruff/check/BUILD
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()
103 changes: 103 additions & 0 deletions src/python/pants/backend/python/lint/ruff/check/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from dataclasses import dataclass
from typing import Any

from pants.backend.python.lint.ruff.check.skip_field import SkipRuffCheckField
from pants.backend.python.lint.ruff.common import RunRuffRequest, run_ruff
from pants.backend.python.lint.ruff.skip_field import SkipRuffField
from pants.backend.python.lint.ruff.subsystem import Ruff, RuffMode
from pants.backend.python.target_types import (
InterpreterConstraintsField,
PythonResolveField,
PythonSourceField,
)
from pants.backend.python.util_rules import pex
from pants.core.goals.fix import FixResult, FixTargetsRequest
from pants.core.goals.lint import LintResult, LintTargetsRequest
from pants.core.util_rules.partitions import PartitionerType
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.rules import Get, collect_rules, rule
from pants.engine.target import FieldSet, Target
from pants.util.logging import LogLevel
from pants.util.meta import classproperty


@dataclass(frozen=True)
class RuffCheckFieldSet(FieldSet):
required_fields = (PythonSourceField,)

source: PythonSourceField
resolve: PythonResolveField
interpreter_constraints: InterpreterConstraintsField

@classmethod
def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipRuffCheckField).value or tgt.get(SkipRuffField).value


class RuffLintRequest(LintTargetsRequest):
field_set_type = RuffCheckFieldSet
tool_subsystem = Ruff
partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION

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

@classproperty
def tool_id(cls) -> str:
return "ruff-check"


class RuffFixRequest(FixTargetsRequest):
field_set_type = RuffCheckFieldSet
tool_subsystem = Ruff
partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION

# We don't need to include automatically added lint rules for this RuffFixRequest,
# because these lint rules are already checked by RuffLintRequest.
enable_lint_rules = False

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

@classproperty
def tool_id(cls) -> str:
return RuffLintRequest.tool_id


@rule(desc="Fix with `ruff check --fix`", level=LogLevel.DEBUG)
async def ruff_fix(request: RuffFixRequest.Batch, ruff: Ruff) -> FixResult:
result = await run_ruff(
RunRuffRequest(snapshot=request.snapshot, mode=RuffMode.FIX),
ruff,
)
return await FixResult.create(request, result)


@rule(desc="Lint with `ruff check`", level=LogLevel.DEBUG)
async def ruff_lint(
request: RuffLintRequest.Batch[RuffCheckFieldSet, Any], ruff: Ruff
) -> LintResult:
source_files = await Get(
SourceFiles, SourceFilesRequest(field_set.source for field_set in request.elements)
)
result = await run_ruff(
RunRuffRequest(snapshot=source_files.snapshot, mode=RuffMode.LINT),
ruff,
)
return LintResult.create(request, result)


def rules():
return [
*collect_rules(),
*RuffFixRequest.rules(),
*RuffLintRequest.rules(),
*pex.rules(),
]
26 changes: 26 additions & 0 deletions src/python/pants/backend/python/lint/ruff/check/skip_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from pants.backend.python.target_types import (
PythonSourcesGeneratorTarget,
PythonSourceTarget,
PythonTestsGeneratorTarget,
PythonTestTarget,
PythonTestUtilsGeneratorTarget,
)
from pants.engine.target import BoolField


class SkipRuffCheckField(BoolField):
alias = "skip_ruff_check"
default = False
help = "If true, don't run the ruff checker on this target's code."


def rules():
return [
PythonSourcesGeneratorTarget.register_plugin_field(SkipRuffCheckField),
PythonSourceTarget.register_plugin_field(SkipRuffCheckField),
PythonTestsGeneratorTarget.register_plugin_field(SkipRuffCheckField),
PythonTestTarget.register_plugin_field(SkipRuffCheckField),
PythonTestUtilsGeneratorTarget.register_plugin_field(SkipRuffCheckField),
]
147 changes: 0 additions & 147 deletions src/python/pants/backend/python/lint/ruff/check_rules.py

This file was deleted.

Loading

0 comments on commit a67f30e

Please sign in to comment.