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

pantsbuild / pants / 19250292619

11 Nov 2025 12:09AM UTC coverage: 77.865% (-2.4%) from 80.298%
19250292619

push

github

web-flow
flag non-runnable targets used with `code_quality_tool` (#22875)

2 of 5 new or added lines in 2 files covered. (40.0%)

1487 existing lines in 72 files now uncovered.

71448 of 91759 relevant lines covered (77.86%)

3.22 hits per line

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

76.47
/src/python/pants/build_graph/build_file_aliases.py
1
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
11✔
5

6
from collections.abc import Callable, Mapping
11✔
7
from dataclasses import dataclass
11✔
8
from typing import Any
11✔
9

10
from pants.base.parse_context import ParseContext
11✔
11
from pants.util.frozendict import FrozenDict
11✔
12

13
ContextAwareObjectFactory = Callable[[ParseContext], Callable[..., None]]
11✔
14

15

16
@dataclass(frozen=True)
11✔
17
class BuildFileAliases:
11✔
18
    """A structure containing sets of symbols to be exposed in BUILD files.
19

20
    There are three types of symbols that can be directly exposed:
21

22
    :API: public
23

24
    - targets: These are Target subclasses or TargetMacro.Factory instances.
25
    - objects: These are any python object, from constants to types.
26
    - context_aware_object_factories: These are object factories that are passed a ParseContext and
27
      produce one or more objects that use data from the context to enable some feature or utility;
28
      you might call them a BUILD file "macro" since they expand parameters to some final, "real"
29
      BUILD file object.  Common uses include creating objects that must be aware of the current
30
      BUILD file path or functions that need to be able to create targets or objects from within the
31
      BUILD file parse.
32
    """
33

34
    _objects: FrozenDict[str, Any]
11✔
35
    _context_aware_object_factories: FrozenDict[str, ContextAwareObjectFactory]
11✔
36

37
    @classmethod
11✔
38
    def _validate_alias(cls, category: str, alias: str, obj: Any) -> None:
11✔
39
        if not isinstance(alias, str):
11✔
40
            raise TypeError(
×
41
                "Aliases must be strings, given {category} entry {alias!r} of type {typ} as "
42
                "the alias of {obj}".format(
43
                    category=category, alias=alias, typ=type(alias).__name__, obj=obj
44
                )
45
            )
46

47
    @classmethod
11✔
48
    def _validate_objects(cls, objects: dict[str, Any] | None) -> FrozenDict[str, Any]:
11✔
49
        if not objects:
11✔
50
            return FrozenDict()
11✔
51

52
        for alias, obj in objects.items():
11✔
53
            cls._validate_alias("objects", alias, obj)
11✔
54
        return FrozenDict(objects)
11✔
55

56
    @classmethod
11✔
57
    def _validate_context_aware_object_factories(
11✔
58
        cls, context_aware_object_factories: dict[str, ContextAwareObjectFactory] | None
59
    ) -> FrozenDict[str, ContextAwareObjectFactory]:
60
        if not context_aware_object_factories:
11✔
61
            return FrozenDict()
11✔
62

63
        for alias, obj in context_aware_object_factories.items():
2✔
64
            cls._validate_alias("context_aware_object_factories", alias, obj)
2✔
65
            if not callable(obj):
2✔
UNCOV
66
                raise TypeError(
×
67
                    "The given context aware object factory {alias!r} must be a callable.".format(
68
                        alias=alias
69
                    )
70
                )
71

72
        return FrozenDict(context_aware_object_factories)
2✔
73

74
    def __init__(
11✔
75
        self,
76
        objects: dict[str, Any] | None = None,
77
        context_aware_object_factories: dict[str, ContextAwareObjectFactory] | None = None,
78
    ) -> None:
79
        """
80
        :API: public
81
        """
82
        object.__setattr__(self, "_objects", self._validate_objects(objects))
11✔
83
        object.__setattr__(
11✔
84
            self,
85
            "_context_aware_object_factories",
86
            self._validate_context_aware_object_factories(context_aware_object_factories),
87
        )
88

89
    @property
11✔
90
    def objects(self) -> FrozenDict[str, Any]:
11✔
91
        """
92
        :API: public
93
        """
94
        return self._objects
11✔
95

96
    @property
11✔
97
    def context_aware_object_factories(self) -> FrozenDict[str, ContextAwareObjectFactory]:
11✔
98
        """
99
        :API: public
100
        """
101
        return self._context_aware_object_factories
11✔
102

103
    def merge(self, other: BuildFileAliases) -> BuildFileAliases:
11✔
104
        """Merges a set of build file aliases and returns a new set of aliases containing both.
105

106
        Any duplicate aliases from `other` will trump.
107

108
        :API: public
109
        """
UNCOV
110
        if not isinstance(other, BuildFileAliases):
×
111
            raise TypeError(f"Can only merge other BuildFileAliases, given {other}")
×
112

UNCOV
113
        def _merge(item1: Mapping[str, Any], item2: Mapping[str, Any]) -> dict[str, Any]:
×
UNCOV
114
            merged: dict[str, Any] = {}
×
UNCOV
115
            merged.update(item1)
×
UNCOV
116
            merged.update(item2)
×
UNCOV
117
            return merged
×
118

UNCOV
119
        objects = _merge(self.objects, other.objects)
×
UNCOV
120
        context_aware_object_factories = _merge(
×
121
            self.context_aware_object_factories, other.context_aware_object_factories
122
        )
UNCOV
123
        return BuildFileAliases(
×
124
            objects=objects,
125
            context_aware_object_factories=context_aware_object_factories,
126
        )
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