• 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

52.78
/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("/", ".")
×
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 = []
×
47
    if python_protobuf_subsystem.grpcio_plugin:
×
48
        grpc_suffixes.append("_pb2_grpc")
×
49
    if python_protobuf_subsystem.grpclib_plugin:
×
50
        grpc_suffixes.append("_grpc")
×
51

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

57
    resolves_to_modules_to_providers: DefaultDict[
×
58
        ResolveName, DefaultDict[str, list[ModuleProvider]]
59
    ] = defaultdict(lambda: defaultdict(list))
60
    for tgt, stripped_file in zip(protobuf_targets, stripped_file_per_target):
×
61
        resolve = tgt[PythonResolveField].normalized_value(python_setup)
×
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")
×
67
        resolves_to_modules_to_providers[resolve][module].append(
×
68
            ModuleProvider(tgt.address, ModuleProviderType.IMPL)
69
        )
70
        if tgt.get(ProtobufGrpcToggleField).value:
×
71
            for suffix in grpc_suffixes:
×
72
                module = proto_path_to_py_module(stripped_file.value, suffix=suffix)
×
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)
×
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