• 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

85.19
/src/python/pants/engine/collection.py
1
# Copyright 2020 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 Iterable
7✔
7
from typing import Any, ClassVar, TypeVar, cast, overload
7✔
8

9
from pants.util.ordered_set import FrozenOrderedSet
7✔
10

11
T = TypeVar("T")
7✔
12

13

14
class Collection(tuple[T, ...]):
7✔
15
    """A light newtype around immutable sequences for use with the V2 engine.
16

17
    This should be subclassed when you want to create a distinct collection type, such as:
18

19
        @dataclass(frozen=True)
20
        class Example:
21
            val1: str
22

23
        class Examples(Collection[Example]):
24
            pass
25

26
    N.B: Collection instances are only considered equal if both their types and contents are equal.
27
    """
28

29
    @overload  # type: ignore[override]  # noqa: F811
30
    def __getitem__(self, index: int) -> T: ...
31

32
    @overload  # noqa: F811
33
    def __getitem__(self, index: slice) -> Collection[T]: ...
34

35
    def __getitem__(self, index: int | slice) -> T | Collection[T]:  # noqa: F811
7✔
36
        result = super().__getitem__(index)
7✔
37
        if isinstance(index, int):
7✔
38
            return cast(T, result)
7✔
39
        return self.__class__(cast(tuple[T, ...], result))
×
40

41
    def __eq__(self, other: Any) -> bool:
7✔
42
        if self is other:
7✔
43
            return True
×
44
        return type(self) is type(other) and super().__eq__(other)
7✔
45

46
    def __ne__(self, other: Any) -> bool:
7✔
47
        # We must explicitly override to provide the inverse of _our_ __eq__ and not get the
48
        # inverse of tuple.__eq__.
49
        return not self == other
×
50

51
    # Unlike in Python 2 we must explicitly implement __hash__ since we explicitly implement __eq__
52
    # per the Python 3 data model.
53
    # See: https://docs.python.org/3/reference/datamodel.html#object.__hash__
54
    def __hash__(self):
7✔
55
        return super().__hash__()
7✔
56

57
    def __repr__(self) -> str:
7✔
58
        return f"{self.__class__.__name__}({list(self)})"
×
59

60

61
# NB: This name is clunky. It would be more appropriate to call it `Set`, but that is claimed by
62
#  `typing.Set` already. See
63
#  https://github.com/pantsbuild/pants/pull/9590#pullrequestreview-395970440.
64
class DeduplicatedCollection(FrozenOrderedSet[T]):
7✔
65
    """A light newtype around FrozenOrderedSet for use with the V2 engine.
66

67
    This should be subclassed when you want to create a distinct collection type, such as:
68

69
        @dataclass(frozen=True)
70
        class Example:
71
            val1: str
72

73
        class Examples(DeduplicatedCollection[Example]):
74
            pass
75

76
    If it is safe to sort the inputs, you should do so for more cache hits by setting the class
77
    property `sort_input = True`.
78
    """
79

80
    sort_input: ClassVar[bool] = False
7✔
81

82
    def __init__(self, iterable: Iterable[T] = ()) -> None:
7✔
83
        super().__init__(
7✔
84
            iterable if not self.sort_input else sorted(iterable)  # type: ignore[type-var]
85
        )
86

87
    def __repr__(self) -> str:
7✔
88
        return f"{self.__class__.__name__}({list(self._items)})"
7✔
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