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

pantsbuild / pants / 24558864320

17 Apr 2026 09:47AM UTC coverage: 92.923%. First build
24558864320

Pull #23268

github

web-flow
Merge 2f770a0ba into a92bc34b6
Pull Request #23268: pref: Remove python coroutine/trampoline overhead in awaits for ~22% faster `dependencies` goal

30 of 35 new or added lines in 4 files covered. (85.71%)

91651 of 98631 relevant lines covered (92.92%)

4.04 hits per line

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

74.29
/src/python/pants/engine/internals/selectors.py
1
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
12✔
5

6
import itertools
12✔
7
from collections.abc import Coroutine, Iterable
12✔
8
from dataclasses import dataclass
12✔
9
from typing import Any, TypeVar, overload
12✔
10

11
from pants.engine.internals.native_engine import Call as Call  # noqa: F401
12✔
12
from pants.engine.internals.native_engine import _Concurrently
12✔
13
from pants.util.strutil import softwrap
12✔
14

15
_Output = TypeVar("_Output")
12✔
16

17

18
@dataclass(frozen=True)
12✔
19
class AwaitableConstraints:
12✔
20
    rule_id: str
12✔
21
    output_type: type
12✔
22
    # The number of explicit positional arguments passed to a call-by-name awaitable.
23
    explicit_args_arity: int
12✔
24
    input_types: tuple[type, ...]
12✔
25

26
    def __repr__(self) -> str:
12✔
27
        inputs = ", ".join(f"{t.__name__}" for t in self.input_types)
2✔
28
        return f"{self.rule_id}({inputs}) -> {self.output_type.__name__}"
2✔
29

30
    def __str__(self) -> str:
12✔
31
        return repr(self)
2✔
32

33

34
# These type variables are used to parametrize from 1 to 10 args when used in a tuple-style
35
# concurrently() call.
36

37
_Out0 = TypeVar("_Out0")
12✔
38
_Out1 = TypeVar("_Out1")
12✔
39
_Out2 = TypeVar("_Out2")
12✔
40
_Out3 = TypeVar("_Out3")
12✔
41
_Out4 = TypeVar("_Out4")
12✔
42
_Out5 = TypeVar("_Out5")
12✔
43
_Out6 = TypeVar("_Out6")
12✔
44
_Out7 = TypeVar("_Out7")
12✔
45
_Out8 = TypeVar("_Out8")
12✔
46
_Out9 = TypeVar("_Out9")
12✔
47

48

49
@overload
50
def Concurrently(
51
    __gets: Iterable[Coroutine[Any, Any, _Output] | _Concurrently[_Output]],
52
) -> _Concurrently[tuple[_Output, ...]]: ...
53

54

55
@overload
56
def Concurrently(
57
    __get0: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
58
    __get1: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
59
    __get2: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
60
    __get3: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
61
    __get4: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
62
    __get5: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
63
    __get6: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
64
    __get7: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
65
    __get8: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
66
    __get9: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
67
    __get10: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
68
    *__gets: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
69
) -> _Concurrently[tuple[_Output, ...]]: ...
70

71

72
@overload
12✔
73
def Concurrently(
12✔
74
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
75
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
76
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
77
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
78
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
79
    __get5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5],
80
    __get6: Coroutine[Any, Any, _Out6] | _Concurrently[_Out6],
81
    __get7: Coroutine[Any, Any, _Out7] | _Concurrently[_Out7],
82
    __get8: Coroutine[Any, Any, _Out8] | _Concurrently[_Out8],
83
    __get9: Coroutine[Any, Any, _Out9] | _Concurrently[_Out9],
84
) -> _Concurrently[
85
    tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7, _Out8, _Out9]
86
]: ...
87

88

89
@overload
90
def Concurrently(
91
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
92
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
93
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
94
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
95
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
96
    __get5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5],
97
    __get6: Coroutine[Any, Any, _Out6] | _Concurrently[_Out6],
98
    __get7: Coroutine[Any, Any, _Out7] | _Concurrently[_Out7],
99
    __get8: Coroutine[Any, Any, _Out8] | _Concurrently[_Out8],
100
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7, _Out8]]: ...
101

102

103
@overload
104
def Concurrently(
105
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
106
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
107
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
108
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
109
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
110
    __get5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5],
111
    __get6: Coroutine[Any, Any, _Out6] | _Concurrently[_Out6],
112
    __get7: Coroutine[Any, Any, _Out7] | _Concurrently[_Out7],
113
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7]]: ...
114

115

116
@overload
117
def Concurrently(
118
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
119
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
120
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
121
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
122
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
123
    __get5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5],
124
    __get6: Coroutine[Any, Any, _Out6] | _Concurrently[_Out6],
125
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6]]: ...
126

127

128
@overload
129
def Concurrently(
130
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
131
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
132
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
133
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
134
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
135
    __get5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5],
136
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5]]: ...
137

138

139
@overload
140
def Concurrently(
141
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
142
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
143
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
144
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
145
    __get4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4],
146
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3, _Out4]]: ...
147

148

149
@overload
150
def Concurrently(
151
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
152
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
153
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
154
    __get3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3],
155
) -> _Concurrently[tuple[_Out0, _Out1, _Out2, _Out3]]: ...
156

157

158
@overload
159
def Concurrently(
160
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
161
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
162
    __get2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2],
163
) -> _Concurrently[tuple[_Out0, _Out1, _Out2]]: ...
164

165

166
@overload
167
def Concurrently(
168
    __get0: Coroutine[Any, Any, _Out0] | _Concurrently[_Out0],
169
    __get1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1],
170
) -> _Concurrently[tuple[_Out0, _Out1]]: ...
171

172

173
def Concurrently(
12✔
174
    __arg0: (Iterable[Coroutine[Any, Any, _Output] | _Concurrently[_Output]] | Coroutine[Any, Any, _Out0] | _Concurrently[_Out0]),
175
    __arg1: Coroutine[Any, Any, _Out1] | _Concurrently[_Out1] | None = None,
176
    __arg2: Coroutine[Any, Any, _Out2] | _Concurrently[_Out2] | None = None,
177
    __arg3: Coroutine[Any, Any, _Out3] | _Concurrently[_Out3] | None = None,
178
    __arg4: Coroutine[Any, Any, _Out4] | _Concurrently[_Out4] | None = None,
179
    __arg5: Coroutine[Any, Any, _Out5] | _Concurrently[_Out5] | None = None,
180
    __arg6: Coroutine[Any, Any, _Out6] | _Concurrently[_Out6] | None = None,
181
    __arg7: Coroutine[Any, Any, _Out7] | _Concurrently[_Out7] | None = None,
182
    __arg8: Coroutine[Any, Any, _Out8] | _Concurrently[_Out8] | None = None,
183
    __arg9: Coroutine[Any, Any, _Out9] | _Concurrently[_Out9] | None = None,
184
    *__args: Coroutine[Any, Any, _Output] | _Concurrently[_Output],
185
) -> _Concurrently[
186
    tuple[_Output, ...]
187
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7, _Out8, _Out9]
188
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7, _Out8]
189
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6, _Out7]
190
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5, _Out6]
191
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4, _Out5]
192
    | tuple[_Out0, _Out1, _Out2, _Out3, _Out4]
193
    | tuple[_Out0, _Out1, _Out2, _Out3]
194
    | tuple[_Out0, _Out1, _Out2]
195
    | tuple[_Out0, _Out1]
196
    | tuple[_Out0]
197
]:
198
    """Yield a tuple of Coroutine instances all at once.
199

200
    The `yield`ed value `self.calls` is interpreted by the engine within
201
    `generator_send()`. This class will yield a tuple of Coroutine instances,
202
    which is converted into `PyGeneratorResponse::GetMulti`.
203

204
    The engine will fulfill these coroutines in parallel, and return a tuple of _Output
205
    instances to this method, which then returns this tuple to the `@rule` which called
206
    `await concurrently(...) for ... in ...)`.
207
    """
208
    if (
12✔
209
        isinstance(__arg0, Iterable)
210
        and __arg1 is None
211
        and __arg2 is None
212
        and __arg3 is None
213
        and __arg4 is None
214
        and __arg5 is None
215
        and __arg6 is None
216
        and __arg7 is None
217
        and __arg8 is None
218
        and __arg9 is None
219
        and not __args
220
    ):
221
        return _Concurrently(tuple(__arg0))
12✔
222

223
    if (
12✔
224
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
225
        and __arg1 is None
226
        and __arg2 is None
227
        and __arg3 is None
228
        and __arg4 is None
229
        and __arg5 is None
230
        and __arg6 is None
231
        and __arg7 is None
232
        and __arg8 is None
233
        and __arg9 is None
234
        and not __args
235
    ):
236
        return _Concurrently((__arg0,))
×
237

238
    if (
12✔
239
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
240
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
241
        and __arg2 is None
242
        and __arg3 is None
243
        and __arg4 is None
244
        and __arg5 is None
245
        and __arg6 is None
246
        and __arg7 is None
247
        and __arg8 is None
248
        and __arg9 is None
249
        and not __args
250
    ):
251
        return _Concurrently((__arg0, __arg1))
12✔
252

253
    if (
12✔
254
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
255
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
256
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
257
        and __arg3 is None
258
        and __arg4 is None
259
        and __arg5 is None
260
        and __arg6 is None
261
        and __arg7 is None
262
        and __arg8 is None
263
        and __arg9 is None
264
        and not __args
265
    ):
266
        return _Concurrently((__arg0, __arg1, __arg2))
12✔
267

268
    if (
12✔
269
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
270
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
271
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
272
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
273
        and __arg4 is None
274
        and __arg5 is None
275
        and __arg6 is None
276
        and __arg7 is None
277
        and __arg8 is None
278
        and __arg9 is None
279
        and not __args
280
    ):
281
        return _Concurrently((__arg0, __arg1, __arg2, __arg3))
12✔
282

283
    if (
7✔
284
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
285
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
286
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
287
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
288
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
289
        and __arg5 is None
290
        and __arg6 is None
291
        and __arg7 is None
292
        and __arg8 is None
293
        and __arg9 is None
294
        and not __args
295
    ):
296
        return _Concurrently((__arg0, __arg1, __arg2, __arg3, __arg4))
5✔
297

298
    if (
4✔
299
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
300
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
301
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
302
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
303
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
304
        and isinstance(__arg5, (Coroutine, Call, _Concurrently))
305
        and __arg6 is None
306
        and __arg7 is None
307
        and __arg8 is None
308
        and __arg9 is None
309
        and not __args
310
    ):
311
        return _Concurrently((__arg0, __arg1, __arg2, __arg3, __arg4, __arg5))
4✔
312

NEW
313
    if (
×
314
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
315
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
316
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
317
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
318
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
319
        and isinstance(__arg5, (Coroutine, Call, _Concurrently))
320
        and isinstance(__arg6, (Coroutine, Call, _Concurrently))
321
        and __arg7 is None
322
        and __arg8 is None
323
        and __arg9 is None
324
        and not __args
325
    ):
326
        return _Concurrently((__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6))
×
327

NEW
328
    if (
×
329
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
330
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
331
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
332
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
333
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
334
        and isinstance(__arg5, (Coroutine, Call, _Concurrently))
335
        and isinstance(__arg6, (Coroutine, Call, _Concurrently))
336
        and isinstance(__arg7, (Coroutine, Call, _Concurrently))
337
        and __arg8 is None
338
        and __arg9 is None
339
        and not __args
340
    ):
341
        return _Concurrently((__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7))
×
342

NEW
343
    if (
×
344
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
345
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
346
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
347
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
348
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
349
        and isinstance(__arg5, (Coroutine, Call, _Concurrently))
350
        and isinstance(__arg6, (Coroutine, Call, _Concurrently))
351
        and isinstance(__arg7, (Coroutine, Call, _Concurrently))
352
        and isinstance(__arg8, (Coroutine, Call, _Concurrently))
353
        and __arg9 is None
354
        and not __args
355
    ):
356
        return _Concurrently(
×
357
            (__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8)
358
        )
359

360
    if (
×
361
        isinstance(__arg0, (Coroutine, Call, _Concurrently))
362
        and isinstance(__arg1, (Coroutine, Call, _Concurrently))
363
        and isinstance(__arg2, (Coroutine, Call, _Concurrently))
364
        and isinstance(__arg3, (Coroutine, Call, _Concurrently))
365
        and isinstance(__arg4, (Coroutine, Call, _Concurrently))
366
        and isinstance(__arg5, (Coroutine, Call, _Concurrently))
367
        and isinstance(__arg6, (Coroutine, Call, _Concurrently))
368
        and isinstance(__arg7, (Coroutine, Call, _Concurrently))
369
        and isinstance(__arg8, (Coroutine, Call, _Concurrently))
370
        and isinstance(__arg9, (Coroutine, Call, _Concurrently))
371
        and all(isinstance(arg, (Coroutine, Call, _Concurrently)) for arg in __args)
372
    ):
NEW
373
        return _Concurrently(
×
374
            (
375
                __arg0,
376
                __arg1,
377
                __arg2,
378
                __arg3,
379
                __arg4,
380
                __arg5,
381
                __arg6,
382
                __arg7,
383
                __arg8,
384
                __arg9,
385
                *__args,
386
            )
387
        )
388

389
    args = __arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8, __arg9, *__args
×
390

391
    def render_arg(arg: Any) -> str | None:
×
392
        if arg is None:
×
393
            return None
×
394
        return repr(arg)
×
395

396
    likely_args_explicitly_passed = tuple(
×
397
        reversed(
398
            [
399
                render_arg(arg)
400
                for arg in itertools.dropwhile(lambda arg: arg is None, reversed(args))
401
            ]
402
        )
403
    )
404
    if any(arg is None for arg in likely_args_explicitly_passed):
×
405
        raise ValueError(
×
406
            softwrap(
407
                f"""
408
                Unexpected concurrently() None arguments: {
409
                    ", ".join(map(str, likely_args_explicitly_passed))
410
                }
411

412
                When calling concurrently() on individual rule calls, all leading arguments must be
413
                awaitables.
414
                """
415
            )
416
        )
417

418
    raise TypeError(
×
419
        softwrap(
420
            f"""
421
            Unexpected concurrently() argument types: {", ".join(map(str, likely_args_explicitly_passed))}
422

423
            `concurrently` can be used in two ways:
424
              1. concurrently(Iterable[awaitable[T]]) -> Tuple[T]
425
              2. concurrently(awaitable[T1]], ...) -> Tuple[T1, T2, ...]
426

427
            The 1st form is intended for homogenous collections of rule calls and emulates an
428
            async `for ...` comprehension used to iterate over the collection in parallel and
429
            collect the results in a homogenous tuple when all are complete.
430

431
            The 2nd form supports executing heterogeneous rule calls in parallel and collecting
432
            them in a heterogeneous tuple when all are complete. Currently up to 10 heterogeneous
433
            rule calls can be passed while still tracking their output types for type-checking by
434
            MyPy and similar type checkers. If more than 10 rule calls are passed, type checking
435
            will enforce that they are homogeneous.
436
            """
437
        )
438
    )
439

440

441
concurrently = Concurrently
12✔
442

443

444
@dataclass(frozen=True)
12✔
445
class Params:
12✔
446
    """A set of values with distinct types.
447

448
    Distinct types are enforced at consumption time by the rust type of the same name.
449
    """
450

451
    params: tuple[Any, ...]
12✔
452

453
    def __init__(self, *args: Any) -> None:
12✔
454
        object.__setattr__(self, "params", tuple(args))
12✔
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