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

pantsbuild / pants / 20632486505

01 Jan 2026 04:21AM UTC coverage: 43.231% (-37.1%) from 80.281%
20632486505

Pull #22962

github

web-flow
Merge 08d5c63b0 into f52ab6675
Pull Request #22962: Bump the gha-deps group across 1 directory with 6 updates

26122 of 60424 relevant lines covered (43.23%)

0.86 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
2✔
5

6
import operator
2✔
7
from collections.abc import Callable, Iterable
2✔
8
from typing import TYPE_CHECKING, TypeVar
2✔
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")
2✔
18
Filter = Callable[[_T], bool]
2✔
19
TargetFilter = Callable[["Target"], bool]
2✔
20

21

22
def _extract_modifier(modified_param: str) -> tuple[Callable[[bool], bool], str]:
2✔
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:
2✔
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(
2✔
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:
2✔
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

© 2026 Coveralls, Inc