• 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/goals/run_python_source.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

UNCOV
4
from dataclasses import dataclass
×
UNCOV
5
from pathlib import PurePath
×
6

UNCOV
7
from pants.backend.python.goals.run_helper import (
×
8
    _create_python_source_run_dap_request,
9
    _create_python_source_run_request,
10
)
UNCOV
11
from pants.backend.python.subsystems.debugpy import DebugPy
×
UNCOV
12
from pants.backend.python.subsystems.setup import PythonSetup
×
UNCOV
13
from pants.backend.python.target_types import (
×
14
    Executable,
15
    InterpreterConstraintsField,
16
    PexEntryPointField,
17
    PythonResolveField,
18
    PythonRunGoalUseSandboxField,
19
    PythonSourceField,
20
)
UNCOV
21
from pants.backend.python.target_types_rules import rules as python_target_types_rules
×
UNCOV
22
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
×
UNCOV
23
from pants.backend.python.util_rules.pex import create_pex
×
UNCOV
24
from pants.backend.python.util_rules.pex_environment import PexEnvironment
×
UNCOV
25
from pants.backend.python.util_rules.pex_from_targets import rules as pex_from_targets_rules
×
UNCOV
26
from pants.core.goals.run import (
×
27
    RunDebugAdapterRequest,
28
    RunFieldSet,
29
    RunInSandboxBehavior,
30
    RunInSandboxRequest,
31
    RunRequest,
32
)
UNCOV
33
from pants.core.subsystems.debug_adapter import DebugAdapterSubsystem
×
UNCOV
34
from pants.engine.rules import collect_rules, rule
×
UNCOV
35
from pants.util.logging import LogLevel
×
36

37

UNCOV
38
@dataclass(frozen=True)
×
UNCOV
39
class PythonSourceFieldSet(RunFieldSet):
×
UNCOV
40
    supports_debug_adapter = True
×
UNCOV
41
    required_fields = (PythonSourceField, PythonRunGoalUseSandboxField)
×
UNCOV
42
    run_in_sandbox_behavior = RunInSandboxBehavior.CUSTOM
×
43

44
    source: PythonSourceField
45
    interpreter_constraints: InterpreterConstraintsField
46
    resolve: PythonResolveField
47
    _run_goal_use_sandbox: PythonRunGoalUseSandboxField
48

UNCOV
49
    def should_use_sandbox(self, python_setup: PythonSetup) -> bool:
×
50
        if self._run_goal_use_sandbox.value is None:
×
51
            return python_setup.default_run_goal_use_sandbox
×
52
        return self._run_goal_use_sandbox.value
×
53

UNCOV
54
    def _executable_main(self) -> Executable | None:
×
55
        source = PurePath(self.source.value)
×
56
        source_name = source.stem if source.suffix == ".py" else source.name
×
57
        if not all(part.isidentifier() for part in source_name.split(".")):
×
58
            # If the python source is not importable (python modules can't be named with '-'),
59
            # then it must be an executable script.
60
            executable = Executable.create(self.address, self.source.value)
×
61
        else:
62
            # The module is importable, so entry_point will do the heavy lifting instead.
63
            executable = None
×
64
        return executable
×
65

66

UNCOV
67
@rule(level=LogLevel.DEBUG)
×
UNCOV
68
async def create_python_source_run_request(
×
69
    field_set: PythonSourceFieldSet, pex_env: PexEnvironment, python_setup: PythonSetup
70
) -> RunRequest:
71
    return await _create_python_source_run_request(
×
72
        field_set.address,
73
        entry_point_field=PexEntryPointField(field_set.source.value, field_set.address),
74
        executable=field_set._executable_main(),
75
        pex_env=pex_env,
76
        run_in_sandbox=field_set.should_use_sandbox(python_setup),
77
    )
78

79

UNCOV
80
@rule(level=LogLevel.DEBUG)
×
UNCOV
81
async def create_python_source_run_in_sandbox_request(
×
82
    field_set: PythonSourceFieldSet, pex_env: PexEnvironment, python_setup: PythonSetup
83
) -> RunInSandboxRequest:
84
    # Unlike for `RunRequest`s, `run_in_sandbox` should _always_ be true when running in the
85
    # sandbox.
86
    run_request = await _create_python_source_run_request(
×
87
        field_set.address,
88
        entry_point_field=PexEntryPointField(field_set.source.value, field_set.address),
89
        executable=field_set._executable_main(),
90
        pex_env=pex_env,
91
        run_in_sandbox=True,
92
    )
93
    return run_request.to_run_in_sandbox_request()
×
94

95

UNCOV
96
@rule
×
UNCOV
97
async def create_python_source_debug_adapter_request(
×
98
    field_set: PythonSourceFieldSet,
99
    debugpy: DebugPy,
100
    debug_adapter: DebugAdapterSubsystem,
101
    pex_env: PexEnvironment,
102
    python_setup: PythonSetup,
103
) -> RunDebugAdapterRequest:
104
    debugpy_pex = await create_pex(
×
105
        debugpy.to_pex_request(
106
            interpreter_constraints=InterpreterConstraints.create_from_field_sets(
107
                [field_set], python_setup
108
            )
109
        )
110
    )
111

112
    run_in_sandbox = field_set.should_use_sandbox(python_setup)
×
113
    run_request = await _create_python_source_run_request(
×
114
        field_set.address,
115
        entry_point_field=PexEntryPointField(field_set.source.value, field_set.address),
116
        executable=field_set._executable_main(),
117
        pex_env=pex_env,
118
        pex_path=[debugpy_pex],
119
        run_in_sandbox=run_in_sandbox,
120
    )
121

122
    return await _create_python_source_run_dap_request(
×
123
        run_request,
124
        debugpy=debugpy,
125
        debug_adapter=debug_adapter,
126
        run_in_sandbox=run_in_sandbox,
127
    )
128

129

UNCOV
130
def rules():
×
UNCOV
131
    return [
×
132
        *collect_rules(),
133
        *PythonSourceFieldSet.rules(),
134
        *pex_from_targets_rules(),
135
        *python_target_types_rules(),
136
    ]
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