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

pantsbuild / pants / 23068140808

13 Mar 2026 07:56PM UTC coverage: 52.677% (-40.3%) from 92.932%
23068140808

Pull #23170

github

web-flow
Merge e8ca01cfa into f07276df6
Pull Request #23170: Debug reapi test cache misses

31687 of 60153 relevant lines covered (52.68%)

1.05 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
2✔
5

6
import pytest
2✔
7

8

9
@pytest.fixture(autouse=True, scope="session")
2✔
10
def dedicated_target_fields():
2✔
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
2✔
21

22
    for cls in Target.__subclasses__():
2✔
23
        if hasattr(cls, "core_fields"):
2✔
24
            for field_cls in cls.core_fields:
2✔
25
                # NB: We want to check for all kinds of SourcesFields, like SingleSourceField and
26
                # MultipleSourcesField.
27
                if (
2✔
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 (
2✔
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:
2✔
44
    """Ensures that calls to `object.__setattr__` in a frozen dataclass' `__init__` are valid."""
45

46
    actual_dataclass_decorator = dataclasses.dataclass
2✔
47

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

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

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

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

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

71
            return dataclass_cls
2✔
72

73
        return wrapper
2✔
74

75
    dataclasses.dataclass = new_dataclass_decorator
2✔
76

77

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