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

pantsbuild / pants / 22285099215

22 Feb 2026 08:52PM UTC coverage: 75.854% (-17.1%) from 92.936%
22285099215

Pull #23121

github

web-flow
Merge c7299df9c into ba8359840
Pull Request #23121: fix issue with optional fields in dependency validator

28 of 29 new or added lines in 2 files covered. (96.55%)

11174 existing lines in 400 files now uncovered.

53694 of 70786 relevant lines covered (75.85%)

1.88 hits per line

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

88.89
/src/python/pants/backend/codegen/protobuf/python/python_protobuf_module_mapper.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
2✔
5

6
from collections import defaultdict
2✔
7
from typing import DefaultDict
2✔
8

9
from pants.backend.codegen.protobuf.python.python_protobuf_subsystem import PythonProtobufSubsystem
2✔
10
from pants.backend.codegen.protobuf.target_types import (
2✔
11
    AllProtobufTargets,
12
    ProtobufGrpcToggleField,
13
    ProtobufSourceField,
14
)
15
from pants.backend.python.dependency_inference.module_mapper import (
2✔
16
    FirstPartyPythonMappingImpl,
17
    FirstPartyPythonMappingImplMarker,
18
    ModuleProvider,
19
    ModuleProviderType,
20
    ResolveName,
21
)
22
from pants.backend.python.subsystems.setup import PythonSetup
2✔
23
from pants.backend.python.target_types import PythonResolveField
2✔
24
from pants.core.util_rules.stripped_source_files import StrippedFileNameRequest, strip_file_name
2✔
25
from pants.engine.rules import collect_rules, concurrently, rule
2✔
26
from pants.engine.unions import UnionRule
2✔
27
from pants.util.logging import LogLevel
2✔
28

29

30
def proto_path_to_py_module(stripped_path: str, *, suffix: str) -> str:
2✔
31
    return stripped_path.replace(".proto", suffix).replace("/", ".")
1✔
32

33

34
# This is only used to register our implementation with the plugin hook via unions.
35
class PythonProtobufMappingMarker(FirstPartyPythonMappingImplMarker):
2✔
36
    pass
2✔
37

38

39
@rule(desc="Creating map of Protobuf targets to generated Python modules", level=LogLevel.DEBUG)
2✔
40
async def map_protobuf_to_python_modules(
2✔
41
    protobuf_targets: AllProtobufTargets,
42
    python_setup: PythonSetup,
43
    python_protobuf_subsystem: PythonProtobufSubsystem,
44
    _: PythonProtobufMappingMarker,
45
) -> FirstPartyPythonMappingImpl:
46
    grpc_suffixes = []
1✔
47
    if python_protobuf_subsystem.grpcio_plugin:
1✔
48
        grpc_suffixes.append("_pb2_grpc")
1✔
49
    if python_protobuf_subsystem.grpclib_plugin:
1✔
UNCOV
50
        grpc_suffixes.append("_grpc")
×
51

52
    stripped_file_per_target = await concurrently(
1✔
53
        strip_file_name(StrippedFileNameRequest(tgt[ProtobufSourceField].file_path))
54
        for tgt in protobuf_targets
55
    )
56

57
    resolves_to_modules_to_providers: DefaultDict[
1✔
58
        ResolveName, DefaultDict[str, list[ModuleProvider]]
59
    ] = defaultdict(lambda: defaultdict(list))
60
    for tgt, stripped_file in zip(protobuf_targets, stripped_file_per_target):
1✔
61
        resolve = tgt[PythonResolveField].normalized_value(python_setup)
1✔
62

63
        # NB: We don't consider the MyPy plugin, which generates `_pb2.pyi`. The stubs end up
64
        # sharing the same module as the implementation `_pb2.py`. Because both generated files
65
        # come from the same original Protobuf target, we're covered.
66
        module = proto_path_to_py_module(stripped_file.value, suffix="_pb2")
1✔
67
        resolves_to_modules_to_providers[resolve][module].append(
1✔
68
            ModuleProvider(tgt.address, ModuleProviderType.IMPL)
69
        )
70
        if tgt.get(ProtobufGrpcToggleField).value:
1✔
UNCOV
71
            for suffix in grpc_suffixes:
×
UNCOV
72
                module = proto_path_to_py_module(stripped_file.value, suffix=suffix)
×
UNCOV
73
                resolves_to_modules_to_providers[resolve][module].append(
×
74
                    ModuleProvider(tgt.address, ModuleProviderType.IMPL)
75
                )
76

77
    return FirstPartyPythonMappingImpl.create(resolves_to_modules_to_providers)
1✔
78

79

80
def rules():
2✔
81
    return (
2✔
82
        *collect_rules(),
83
        UnionRule(FirstPartyPythonMappingImplMarker, PythonProtobufMappingMarker),
84
    )
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