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

pantsbuild / pants / 24145945949

08 Apr 2026 04:14PM UTC coverage: 82.077% (-10.8%) from 92.91%
24145945949

Pull #23233

github

web-flow
Merge 089d98e3c into 9036734c9
Pull Request #23233: Introduce a LockfileFormat enum.

8 of 11 new or added lines in 4 files covered. (72.73%)

7635 existing lines in 306 files now uncovered.

63732 of 77649 relevant lines covered (82.08%)

2.96 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
3✔
5

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

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

29

30
def proto_path_to_py_module(stripped_path: str, *, suffix: str) -> str:
3✔
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):
3✔
36
    pass
3✔
37

38

39
@rule(desc="Creating map of Protobuf targets to generated Python modules", level=LogLevel.DEBUG)
3✔
40
async def map_protobuf_to_python_modules(
3✔
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():
3✔
81
    return (
3✔
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