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

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

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

0.0
/src/python/pants/backend/python/lint/black/rules.py
1
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

UNCOV
4
from __future__ import annotations
×
5

UNCOV
6
from typing import cast
×
7

UNCOV
8
from pants.backend.python.lint.black.subsystem import Black, BlackFieldSet
×
UNCOV
9
from pants.backend.python.subsystems.setup import PythonSetup
×
UNCOV
10
from pants.backend.python.util_rules import pex
×
UNCOV
11
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
×
UNCOV
12
from pants.backend.python.util_rules.pex import VenvPexProcess, create_venv_pex
×
UNCOV
13
from pants.core.goals.fmt import AbstractFmtRequest, FmtResult, FmtTargetsRequest, Partitions
×
UNCOV
14
from pants.core.util_rules.config_files import find_config_file
×
UNCOV
15
from pants.engine.fs import MergeDigests
×
UNCOV
16
from pants.engine.intrinsics import merge_digests
×
UNCOV
17
from pants.engine.process import execute_process_or_raise
×
UNCOV
18
from pants.engine.rules import collect_rules, concurrently, implicitly, rule
×
UNCOV
19
from pants.util.logging import LogLevel
×
UNCOV
20
from pants.util.strutil import pluralize, softwrap
×
21

22

UNCOV
23
class BlackRequest(FmtTargetsRequest):
×
UNCOV
24
    field_set_type = BlackFieldSet
×
UNCOV
25
    tool_subsystem = Black  # type: ignore[assignment]
×
26

27

UNCOV
28
async def _run_black(
×
29
    request: AbstractFmtRequest.Batch,
30
    black: Black,
31
    interpreter_constraints: InterpreterConstraints,
32
) -> FmtResult:
33
    black_pex_get = create_venv_pex(
×
34
        **implicitly(black.to_pex_request(interpreter_constraints=interpreter_constraints))
35
    )
36
    config_files_get = find_config_file(black.config_request(request.snapshot.dirs))
×
37
    black_pex, config_files = await concurrently(black_pex_get, config_files_get)
×
38

39
    input_digest = await merge_digests(
×
40
        MergeDigests((request.snapshot.digest, config_files.snapshot.digest))
41
    )
42
    result = await execute_process_or_raise(
×
43
        **implicitly(
44
            VenvPexProcess(
45
                black_pex,
46
                argv=(
47
                    *(("--config", black.config) if black.config else ()),
48
                    "-W",
49
                    "{pants_concurrency}",
50
                    *black.args,
51
                    *request.files,
52
                ),
53
                input_digest=input_digest,
54
                output_files=request.files,
55
                # Note - the cache directory is not used by Pants,
56
                # and we pass through a temporary directory to neutralize
57
                # Black's caching behavior in favor of Pants' caching.
58
                extra_env={"BLACK_CACHE_DIR": "__pants_black_cache"},
59
                concurrency_available=len(request.files),
60
                description=f"Run Black on {pluralize(len(request.files), 'file')}.",
61
                level=LogLevel.DEBUG,
62
            )
63
        )
64
    )
65
    return await FmtResult.create(request, result)
×
66

67

UNCOV
68
@rule
×
UNCOV
69
async def partition_black(
×
70
    request: BlackRequest.PartitionRequest, black: Black, python_setup: PythonSetup
71
) -> Partitions:
72
    if black.skip:
×
73
        return Partitions()
×
74

75
    # Black requires 3.6+ but uses the typed-ast library to work with 2.7, 3.4, 3.5, 3.6, and 3.7.
76
    # However, typed-ast does not understand 3.8+, so instead we must run Black with Python 3.8+
77
    # when relevant. We only do this if <3.8 can't be used, as we don't want a loose requirement
78
    # like `>=3.6` to result in requiring Python 3.8, which would error if 3.8 is not installed on
79
    # the machine.
80
    tool_interpreter_constraints = black.interpreter_constraints
×
81
    if black.options.is_default("interpreter_constraints"):
×
82
        try:
×
83
            # Don't compute this unless we have to, since it might fail.
84
            all_interpreter_constraints = InterpreterConstraints.create_from_field_sets(
×
85
                request.field_sets, python_setup
86
            )
87
        except ValueError:
×
88
            raise ValueError(
×
89
                softwrap(
90
                    """
91
                    Could not compute an interpreter to run Black on, due to conflicting requirements
92
                    in the repo.
93

94
                    Please set `[black].interpreter_constraints` explicitly in pants.toml to a
95
                    suitable interpreter.
96
                    """
97
                )
98
            )
99
        if all_interpreter_constraints.requires_python38_or_newer(
×
100
            python_setup.interpreter_versions_universe
101
        ):
102
            tool_interpreter_constraints = all_interpreter_constraints
×
103

104
    return Partitions.single_partition(
×
105
        (field_set.source.file_path for field_set in request.field_sets),
106
        metadata=tool_interpreter_constraints,
107
    )
108

109

UNCOV
110
@rule(desc="Format with Black", level=LogLevel.DEBUG)
×
UNCOV
111
async def black_fmt(request: BlackRequest.Batch, black: Black) -> FmtResult:
×
112
    return await _run_black(
×
113
        request, black, cast(InterpreterConstraints, request.partition_metadata)
114
    )
115

116

UNCOV
117
def rules():
×
UNCOV
118
    return (
×
119
        *collect_rules(),
120
        *BlackRequest.rules(),
121
        *pex.rules(),
122
    )
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

© 2025 Coveralls, Inc