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

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

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

35.48
/src/python/pants/util/filtering.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
3✔
5

6
import operator
3✔
7
from collections.abc import Callable, Iterable
3✔
8
from typing import TYPE_CHECKING, TypeVar
3✔
9

10
if TYPE_CHECKING:
11
    from pants.engine.target import Target
12

13
    # Working around pyupgrade erasing this import, breaking mypy
14
    _ = Target
15

16

17
_T = TypeVar("_T")
3✔
18
Filter = Callable[[_T], bool]
3✔
19
TargetFilter = Callable[["Target"], bool]
3✔
20

21

22
def _extract_modifier(modified_param: str) -> tuple[Callable[[bool], bool], str]:
3✔
23
    if modified_param.startswith("-"):
×
24
        return operator.not_, modified_param[1:]
×
25

26
    def identity_func(x):
×
27
        return x
×
28

29
    return identity_func, modified_param[1:] if modified_param.startswith("+") else modified_param
×
30

31

32
def create_filter(predicate_param: str, predicate_factory: Callable[[str], Filter]) -> Filter:
3✔
33
    """Create a filter function from a string parameter.
34

35
    :param predicate_param: Create a filter for this param string. Each string is a
36
                            comma-separated list of arguments to the predicate_factory.
37
                            If the entire comma-separated list is prefixed by a '-' then the
38
                            sense of the resulting filter is inverted.
39
    :param predicate_factory: A function that takes a parameter and returns a predicate, i.e., a
40
                              function that takes a single parameter (of whatever type the filter
41
                              operates on) and returns a boolean.
42
    :return: A filter function of one argument that is the logical OR of the predicates for each of
43
             the comma-separated arguments. If the comma-separated list was prefixed by a '-',
44
             the sense of the filter is inverted.
45

46
    :API: public
47
    """
48
    modifier, param = _extract_modifier(predicate_param)
×
49
    predicates = [predicate_factory(p) for p in param.split(",")]
×
50

51
    def filt(x: _T) -> bool:
×
52
        return modifier(any(pred(x) for pred in predicates))
×
53

54
    return filt
×
55

56

57
def create_filters(
3✔
58
    predicate_params: Iterable[str], predicate_factory: Callable[[str], Filter]
59
) -> list[Filter]:
60
    """Create filter functions from a list of string parameters.
61

62
    :param predicate_params: A list of predicate_param arguments as in `create_filter`.
63
    :param predicate_factory: As in `create_filter`.
64

65
    :API: public
66
    """
67
    filters = []
×
68
    for predicate_param in predicate_params:
×
69
        filters.append(create_filter(predicate_param, predicate_factory))
×
70
    return filters
×
71

72

73
def and_filters(filters: Iterable[Filter]) -> Filter:
3✔
74
    """Returns a single filter that short-circuit ANDs the specified filters.
75

76
    :API: public
77
    """
78

79
    def combined_filter(x: _T) -> bool:
×
80
        for filt in filters:
×
81
            if not filt(x):
×
82
                return False
×
83
        return True
×
84

85
    return combined_filter
×
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