-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split skip ruff field into two (#20626)
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
1 parent
1aed24f
commit a67f30e
Showing
17 changed files
with
408 additions
and
200 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,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
103
src/python/pants/backend/python/lint/ruff/check/rules.py
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,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
26
src/python/pants/backend/python/lint/ruff/check/skip_field.py
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,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
147
src/python/pants/backend/python/lint/ruff/check_rules.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.