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

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

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

45.1
/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
1✔
5

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

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

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

15

16
@dataclass(frozen=True)
1✔
17
class BuildFileAliases:
1✔
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]
1✔
35
    _context_aware_object_factories: FrozenDict[str, ContextAwareObjectFactory]
1✔
36

37
    @classmethod
1✔
38
    def _validate_alias(cls, category: str, alias: str, obj: Any) -> None:
1✔
UNCOV
39
        if not isinstance(alias, str):
×
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
1✔
48
    def _validate_objects(cls, objects: dict[str, Any] | None) -> FrozenDict[str, Any]:
1✔
UNCOV
49
        if not objects:
×
UNCOV
50
            return FrozenDict()
×
51

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

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

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

UNCOV
72
        return FrozenDict(context_aware_object_factories)
×
73

74
    def __init__(
1✔
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
        """
UNCOV
82
        object.__setattr__(self, "_objects", self._validate_objects(objects))
×
UNCOV
83
        object.__setattr__(
×
84
            self,
85
            "_context_aware_object_factories",
86
            self._validate_context_aware_object_factories(context_aware_object_factories),
87
        )
88

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

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

103
    def merge(self, other: BuildFileAliases) -> BuildFileAliases:
1✔
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