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

pantsbuild / pants / 18517631058

15 Oct 2025 04:18AM UTC coverage: 69.207% (-11.1%) from 80.267%
18517631058

Pull #22745

github

web-flow
Merge 642a76ca1 into 99919310e
Pull Request #22745: [windows] Add windows support in the stdio crate.

53815 of 77759 relevant lines covered (69.21%)

2.42 hits per line

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

68.63
/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
7✔
5

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

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

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

15

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

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

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

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

63
        for alias, obj in context_aware_object_factories.items():
×
64
            cls._validate_alias("context_aware_object_factories", alias, obj)
×
65
            if not callable(obj):
×
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)
×
73

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

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

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

103
    def merge(self, other: BuildFileAliases) -> BuildFileAliases:
7✔
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
        """
110
        if not isinstance(other, BuildFileAliases):
×
111
            raise TypeError(f"Can only merge other BuildFileAliases, given {other}")
×
112

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

119
        objects = _merge(self.objects, other.objects)
×
120
        context_aware_object_factories = _merge(
×
121
            self.context_aware_object_factories, other.context_aware_object_factories
122
        )
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