• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 22285099215

22 Feb 2026 08:52PM UTC coverage: 75.854% (-17.1%) from 92.936%
22285099215

Pull #23121

github

web-flow
Merge c7299df9c into ba8359840
Pull Request #23121: fix issue with optional fields in dependency validator

28 of 29 new or added lines in 2 files covered. (96.55%)

11174 existing lines in 400 files now uncovered.

53694 of 70786 relevant lines covered (75.85%)

1.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.5
/src/python/pants/backend/python/lint/pyupgrade/rules.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3
from __future__ import annotations
1✔
4

5
import logging
1✔
6
from dataclasses import dataclass
1✔
7

8
from pants.backend.python.lint.pyupgrade.skip_field import SkipPyUpgradeField
1✔
9
from pants.backend.python.lint.pyupgrade.subsystem import PyUpgrade
1✔
10
from pants.backend.python.target_types import PythonSourceField
1✔
11
from pants.backend.python.util_rules import pex
1✔
12
from pants.backend.python.util_rules.pex import VenvPexProcess, create_venv_pex
1✔
13
from pants.core.goals.fix import FixResult, FixTargetsRequest
1✔
14
from pants.core.util_rules.partitions import PartitionerType
1✔
15
from pants.engine.intrinsics import execute_process
1✔
16
from pants.engine.rules import collect_rules, implicitly, rule
1✔
17
from pants.engine.target import FieldSet, Target
1✔
18
from pants.util.logging import LogLevel
1✔
19
from pants.util.strutil import pluralize, softwrap
1✔
20

21
logger = logging.getLogger(__name__)
1✔
22

23

24
@dataclass(frozen=True)
1✔
25
class PyUpgradeFieldSet(FieldSet):
1✔
26
    required_fields = (PythonSourceField,)
1✔
27

28
    source: PythonSourceField
1✔
29

30
    @classmethod
1✔
31
    def opt_out(cls, tgt: Target) -> bool:
1✔
32
        return tgt.get(SkipPyUpgradeField).value
×
33

34

35
class PyUpgradeRequest(FixTargetsRequest):
1✔
36
    field_set_type = PyUpgradeFieldSet
1✔
37
    tool_subsystem = PyUpgrade  # type: ignore[assignment]
1✔
38
    partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION
1✔
39

40

41
@rule(desc="Fix with pyupgrade", level=LogLevel.DEBUG)
1✔
42
async def pyupgrade_fix(request: PyUpgradeRequest.Batch, pyupgrade: PyUpgrade) -> FixResult:
1✔
43
    pyupgrade_pex = await create_venv_pex(**implicitly(pyupgrade.to_pex_request()))
1✔
44

45
    # NB: Pyupgrade isn't idempotent, but eventually converges. So keep running until it stops
46
    # changing code. See https://github.com/asottile/pyupgrade/issues/703
47
    # (Technically we could not do this. It doesn't break Pants since the next run on the CLI would
48
    # use the new file with the new digest. However that isn't the UX we want for our users.)
49
    input_digest = request.snapshot.digest
1✔
50
    for _ in range(10):  # Give the loop an upper bound to guard against infinite runs
1✔
51
        result = await execute_process(
1✔
52
            **implicitly(
53
                VenvPexProcess(
54
                    pyupgrade_pex,
55
                    argv=(*pyupgrade.args, *request.files),
56
                    input_digest=input_digest,
57
                    output_files=request.files,
58
                    description=f"Run pyupgrade on {pluralize(len(request.files), 'file')}.",
59
                    level=LogLevel.DEBUG,
60
                )
61
            )
62
        )
63
        if input_digest == result.output_digest:
1✔
64
            # Nothing changed, either due to failure or because it is fixed
65
            break
1✔
UNCOV
66
        input_digest = result.output_digest
×
67
    else:
68
        logger.error(
×
69
            softwrap(
70
                """
71
                Pants ran Pyupgrade continuously on the code 10 times and it changed all 10.
72

73
                Pyupgrade is not idempotent, but should eventually converge. This is either a bug in
74
                Pyupgrade, or Pyupgrade is still trying to converge on fixed code.
75
                """
76
            )
77
        )
78

79
    return await FixResult.create(request, result)
1✔
80

81

82
def rules():
1✔
83
    return (
1✔
84
        *collect_rules(),
85
        *PyUpgradeRequest.rules(),
86
        *pex.rules(),
87
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc