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

pantsbuild / pants / 21803785359

08 Feb 2026 07:13PM UTC coverage: 43.3% (-37.0%) from 80.277%
21803785359

Pull #23085

github

web-flow
Merge 7c1cd926d into 40389cc58
Pull Request #23085: A helper method for indexing paths by source root

2 of 6 new or added lines in 1 file covered. (33.33%)

17114 existing lines in 539 files now uncovered.

26075 of 60219 relevant lines covered (43.3%)

0.43 hits per line

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

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

4
import dataclasses
1✔
5

6
import pytest
1✔
7

8

9
@pytest.fixture(autouse=True, scope="session")
1✔
10
def dedicated_target_fields():
1✔
11
    """Ensures we follow our convention of dedicated source and dependencies field per-target.
12

13
    This helps ensure that plugin authors can do dependency inference on _specific_ field types, and
14
    not have to filter targets using generic field types.
15

16
    Note that this can't help us if a target type should use an _even more specialized_ dependencies
17
    field type (E.g. 100 different target subclasses could use 1 custom dependencies field type,
18
    when in reality there should be many more). However, this is a good sanity check.
19
    """
20
    from pants.engine.target import Dependencies, SourcesField, Target
1✔
21

22
    for cls in Target.__subclasses__():
1✔
23
        if hasattr(cls, "core_fields"):
1✔
24
            for field_cls in cls.core_fields:
1✔
25
                # NB: We want to check for all kinds of SourcesFields, like SingleSourceField and
26
                # MultipleSourcesField.
27
                if (
1✔
28
                    issubclass(field_cls, SourcesField)
29
                    and field_cls.__module__ is SourcesField.__module__
30
                ):
31
                    raise ValueError(
×
32
                        f"{cls.__name__} should have a dedicated field type for the source(s) field."
33
                    )
34
                if (
1✔
35
                    issubclass(field_cls, Dependencies)
36
                    and field_cls.__module__ is Dependencies.__module__
37
                ):
38
                    raise ValueError(
×
39
                        f"{cls.__name__} should have a dedicated field type for the dependencies field."
40
                    )
41

42

43
def _check_frozen_dataclass_attributes() -> None:
1✔
44
    """Ensures that calls to `object.__setattr__` in a frozen dataclass' `__init__` are valid."""
45

46
    actual_dataclass_decorator = dataclasses.dataclass
1✔
47

48
    def new_dataclass_decorator(*args, **kwargs):
1✔
49
        if not kwargs.get("frozen", False):
1✔
50
            return actual_dataclass_decorator(*args, **kwargs)
1✔
51

52
        def wrapper(cls):
1✔
53
            dataclass_cls = actual_dataclass_decorator(*args, **kwargs)(cls)
1✔
54

55
            if dataclass_cls.__init__ is cls.__init__:
1✔
56
                old__init__ = getattr(dataclass_cls, "__init__")
1✔
57

58
                def new__init__(self, *args, **kwargs):
1✔
59
                    old__init__(self, *args, **kwargs)
1✔
60
                    expected = sorted(field.name for field in dataclasses.fields(self))
1✔
61
                    if hasattr(self, "__dict__"):
1✔
62
                        actual = sorted(self.__dict__)
1✔
63
                        assert expected == actual
1✔
64
                    else:
UNCOV
65
                        for attrname in self.__slots__:
×
66
                            # The only way to validate it was initialized is to trigger the descriptor.
UNCOV
67
                            getattr(self, attrname)
×
68

69
                setattr(dataclass_cls, "__init__", new__init__)
1✔
70

71
            return dataclass_cls
1✔
72

73
        return wrapper
1✔
74

75
    dataclasses.dataclass = new_dataclass_decorator
1✔
76

77

78
def pytest_configure() -> None:
1✔
79
    _check_frozen_dataclass_attributes()
1✔
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