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

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 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

4
from __future__ import annotations
×
5

6
from dataclasses import dataclass
×
7

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

41

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

46

47
@rule(desc="Generate Python from Thrift", level=LogLevel.DEBUG)
×
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

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

78
    dependencies: ThriftDependenciesField
×
79
    python_resolve: ThriftPythonResolveField
×
80

81

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

85

86
@rule
×
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

127
def rules():
×
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