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

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

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

0.0
/src/python/pants/backend/codegen/thrift/apache/python/rules.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

UNCOV
4
from __future__ import annotations
×
5

UNCOV
6
from dataclasses import dataclass
×
7

UNCOV
8
from pants.backend.codegen.thrift.apache.python import subsystem
×
UNCOV
9
from pants.backend.codegen.thrift.apache.python.additional_fields import ThriftPythonResolveField
×
UNCOV
10
from pants.backend.codegen.thrift.apache.python.subsystem import ThriftPythonSubsystem
×
UNCOV
11
from pants.backend.codegen.thrift.apache.rules import (
×
12
    GenerateThriftSourcesRequest,
13
    generate_apache_thrift_sources,
14
)
UNCOV
15
from pants.backend.codegen.thrift.target_types import ThriftDependenciesField, ThriftSourceField
×
UNCOV
16
from pants.backend.codegen.utils import find_python_runtime_library_or_raise_error
×
UNCOV
17
from pants.backend.python.dependency_inference.module_mapper import (
×
18
    PythonModuleOwnersRequest,
19
    map_module_to_address,
20
)
UNCOV
21
from pants.backend.python.dependency_inference.subsystem import (
×
22
    AmbiguityResolution,
23
    PythonInferSubsystem,
24
)
UNCOV
25
from pants.backend.python.subsystems.setup import PythonSetup
×
UNCOV
26
from pants.backend.python.target_types import PythonSourceField
×
UNCOV
27
from pants.engine.fs import AddPrefix
×
UNCOV
28
from pants.engine.intrinsics import digest_to_snapshot
×
UNCOV
29
from pants.engine.rules import collect_rules, implicitly, rule
×
UNCOV
30
from pants.engine.target import (
×
31
    FieldSet,
32
    GeneratedSources,
33
    GenerateSourcesRequest,
34
    InferDependenciesRequest,
35
    InferredDependencies,
36
)
UNCOV
37
from pants.engine.unions import UnionRule
×
UNCOV
38
from pants.source.source_root import SourceRootRequest, get_source_root
×
UNCOV
39
from pants.util.logging import LogLevel
×
40

41

UNCOV
42
class GeneratePythonFromThriftRequest(GenerateSourcesRequest):
×
UNCOV
43
    input = ThriftSourceField
×
UNCOV
44
    output = PythonSourceField
×
45

46

UNCOV
47
@rule(desc="Generate Python from Thrift", level=LogLevel.DEBUG)
×
UNCOV
48
async def generate_python_from_thrift(
×
49
    request: GeneratePythonFromThriftRequest,
50
    thrift_python: ThriftPythonSubsystem,
51
) -> GeneratedSources:
52
    result = await generate_apache_thrift_sources(
×
53
        GenerateThriftSourcesRequest(
54
            thrift_source_field=request.protocol_target[ThriftSourceField],
55
            lang_id="py",
56
            lang_options=thrift_python.gen_options,
57
            lang_name="Python",
58
        ),
59
        **implicitly(),
60
    )
61

62
    # We must add back the source root for Python imports to work properly. Note that the file
63
    # paths will be different depending on whether `namespace py` was used. See the tests for
64
    # examples.
65
    source_root = await get_source_root(SourceRootRequest.for_target(request.protocol_target))
×
66
    source_root_restored = (
×
67
        await digest_to_snapshot(**implicitly(AddPrefix(result.snapshot.digest, source_root.path)))
68
        if source_root.path != "."
69
        else await digest_to_snapshot(result.snapshot.digest)
70
    )
71
    return GeneratedSources(source_root_restored)
×
72

73

UNCOV
74
@dataclass(frozen=True)
×
UNCOV
75
class ApacheThriftPythonDependenciesInferenceFieldSet(FieldSet):
×
UNCOV
76
    required_fields = (ThriftDependenciesField, ThriftPythonResolveField)
×
77

UNCOV
78
    dependencies: ThriftDependenciesField
×
UNCOV
79
    python_resolve: ThriftPythonResolveField
×
80

81

UNCOV
82
class InferApacheThriftPythonDependencies(InferDependenciesRequest):
×
UNCOV
83
    infer_from = ApacheThriftPythonDependenciesInferenceFieldSet
×
84

85

UNCOV
86
@rule
×
UNCOV
87
async def find_apache_thrift_python_requirement(
×
88
    request: InferApacheThriftPythonDependencies,
89
    thrift_python: ThriftPythonSubsystem,
90
    python_setup: PythonSetup,
91
    python_infer_subsystem: PythonInferSubsystem,
92
) -> InferredDependencies:
93
    if not thrift_python.infer_runtime_dependency:
×
94
        return InferredDependencies([])
×
95

96
    resolve = request.field_set.python_resolve.normalized_value(python_setup)
×
97

98
    locality = None
×
99
    if python_infer_subsystem.ambiguity_resolution == AmbiguityResolution.by_source_root:
×
100
        source_root = await get_source_root(
×
101
            SourceRootRequest.for_address(request.field_set.address)
102
        )
103
        locality = source_root.path
×
104

105
    addresses_for_thrift = await map_module_to_address(
×
106
        PythonModuleOwnersRequest(
107
            "thrift",
108
            resolve=resolve,
109
            locality=locality,
110
        ),
111
        **implicitly(),
112
    )
113

114
    addr = find_python_runtime_library_or_raise_error(
×
115
        addresses_for_thrift,
116
        request.field_set.address,
117
        "thrift",
118
        resolve=resolve,
119
        resolves_enabled=python_setup.enable_resolves,
120
        recommended_requirement_name="thrift",
121
        recommended_requirement_url="https://pypi.org/project/thrift/",
122
        disable_inference_option=f"[{thrift_python.options_scope}].infer_runtime_dependency",
123
    )
124
    return InferredDependencies([addr])
×
125

126

UNCOV
127
def rules():
×
UNCOV
128
    return (
×
129
        *collect_rules(),
130
        *subsystem.rules(),
131
        UnionRule(GenerateSourcesRequest, GeneratePythonFromThriftRequest),
132
        UnionRule(InferDependenciesRequest, InferApacheThriftPythonDependencies),
133
    )
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

© 2025 Coveralls, Inc