• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

pantsbuild / pants / 20328535594

18 Dec 2025 06:46AM UTC coverage: 57.969% (-22.3%) from 80.295%
20328535594

Pull #22954

github

web-flow
Merge ccc9c5409 into 407284c67
Pull Request #22954: free up disk space in runner image

39083 of 67421 relevant lines covered (57.97%)

0.91 hits per line

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

0.0
/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
×
4

5
import logging
×
6
from dataclasses import dataclass
×
7

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

21
logger = logging.getLogger(__name__)
×
22

23

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

28
    source: PythonSourceField
×
29

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

34

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

40

41
@rule(desc="Fix with pyupgrade", level=LogLevel.DEBUG)
×
42
async def pyupgrade_fix(request: PyUpgradeRequest.Batch, pyupgrade: PyUpgrade) -> FixResult:
×
43
    pyupgrade_pex = await create_venv_pex(**implicitly(pyupgrade.to_pex_request()))
×
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
×
50
    for _ in range(10):  # Give the loop an upper bound to guard against infinite runs
×
51
        result = await execute_process(
×
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:
×
64
            # Nothing changed, either due to failure or because it is fixed
65
            break
×
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)
×
80

81

82
def rules():
×
83
    return (
×
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