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

pantsbuild / pants / 19000741080

01 Nov 2025 06:16PM UTC coverage: 80.3% (+0.3%) from 80.004%
19000741080

Pull #22837

github

web-flow
Merge 51f49bc90 into da3fb359e
Pull Request #22837: Updated Treesitter dependencies

77994 of 97128 relevant lines covered (80.3%)

3.35 hits per line

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

68.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

4
from dataclasses import dataclass
9✔
5
from pathlib import PurePath
9✔
6

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

37

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

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

49
    def should_use_sandbox(self, python_setup: PythonSetup) -> bool:
9✔
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

54
    def _executable_main(self) -> Executable | None:
9✔
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

67
@rule(level=LogLevel.DEBUG)
9✔
68
async def create_python_source_run_request(
9✔
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

80
@rule(level=LogLevel.DEBUG)
9✔
81
async def create_python_source_run_in_sandbox_request(
9✔
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

96
@rule
9✔
97
async def create_python_source_debug_adapter_request(
9✔
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

130
def rules():
9✔
131
    return [
8✔
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