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

pantsbuild / pants / 18812500213

26 Oct 2025 03:42AM UTC coverage: 80.284% (+0.005%) from 80.279%
18812500213

Pull #22804

github

web-flow
Merge 2a56fdb46 into 4834308dc
Pull Request #22804: test_shell_command: use correct default cache scope for a test's environment

29 of 31 new or added lines in 2 files covered. (93.55%)

1314 existing lines in 64 files now uncovered.

77900 of 97030 relevant lines covered (80.28%)

3.35 hits per line

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

89.58
/src/python/pants/backend/awslambda/python/target_types.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
import re
3✔
7
from dataclasses import dataclass
3✔
8
from enum import Enum
3✔
9
from re import Match
3✔
10
from typing import ClassVar, cast
3✔
11

12
from pants.backend.python.target_types import PexCompletePlatformsField, PythonResolveField
3✔
13
from pants.backend.python.util_rules.faas import (
3✔
14
    FaaSArchitecture,
15
    PythonFaaSCompletePlatforms,
16
    PythonFaaSDependencies,
17
    PythonFaaSHandlerField,
18
    PythonFaaSKnownRuntime,
19
    PythonFaaSLayoutField,
20
    PythonFaaSPex3VenvCreateExtraArgsField,
21
    PythonFaaSPexBuildExtraArgs,
22
    PythonFaaSRuntimeField,
23
)
24
from pants.backend.python.util_rules.faas import rules as faas_rules
3✔
25
from pants.core.environments.target_types import EnvironmentField
3✔
26
from pants.core.goals.package import OutputPathField
3✔
27
from pants.engine.addresses import Address
3✔
28
from pants.engine.rules import collect_rules
3✔
29
from pants.engine.target import (
3✔
30
    COMMON_TARGET_FIELDS,
31
    BoolField,
32
    Field,
33
    InvalidFieldException,
34
    StringField,
35
    Target,
36
)
37
from pants.util.docutil import doc_url
3✔
38
from pants.util.strutil import help_text, softwrap
3✔
39

40

41
class PythonAwsLambdaHandlerField(PythonFaaSHandlerField):
3✔
42
    # This doesn't matter (just needs to be fixed), but is the default name used by the AWS
43
    # console when creating a Python lambda, so is as good as any
44
    # https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
45
    reexported_handler_module = "lambda_function"
3✔
46

47
    help = help_text(
3✔
48
        f"""
49
        Entry point to the AWS Lambda handler.
50

51
        {PythonFaaSHandlerField.help}
52

53
        This is re-exported at `{reexported_handler_module}.handler` in the resulting package to be
54
        used as the configured handler of the Lambda in AWS. It can also be accessed under its
55
        source-root-relative module path, for example: `path.to.module.handler_func`.
56
        """
57
    )
58

59

60
@dataclass(frozen=True)
3✔
61
class ResolvedPythonAwsHandler:
3✔
62
    val: str
3✔
63
    file_name_used: bool
3✔
64

65

66
@dataclass(frozen=True)
3✔
67
class ResolvePythonAwsHandlerRequest:
3✔
68
    field: PythonAwsLambdaHandlerField
3✔
69

70

71
class PythonAwsLambdaIncludeRequirements(BoolField):
3✔
72
    alias = "include_requirements"
3✔
73
    default = True
3✔
74
    help = help_text(
3✔
75
        """
76
        Whether to resolve requirements and include them in the AWS Lambda artifact. This is most useful with Lambda
77
        Layers to make code uploads smaller when third-party requirements are in layers.
78
        https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
79
        """
80
    )
81

82

83
class PythonAwsLambdaIncludeSources(BoolField):
3✔
84
    alias = "include_sources"
3✔
85
    default = True
3✔
86
    help = help_text(
3✔
87
        """
88
        Whether to resolve first party sources and include them in the AWS Lambda artifact. This is
89
        most useful to allow creating a Lambda Layer with only third-party requirements.
90
        https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
91
        """
92
    )
93

94

95
PYTHON_RUNTIME_REGEX = r"python(?P<major>\d)\.(?P<minor>\d+)"
3✔
96

97

98
class PythonAwsLambdaFunctionRuntimes(Enum):
3✔
99
    PYTHON_36 = "python3.6"
3✔
100
    PYTHON_37 = "python3.7"
3✔
101
    PYTHON_38 = "python3.8"
3✔
102
    PYTHON_39 = "python3.9"
3✔
103
    PYTHON_310 = "python3.10"
3✔
104
    PYTHON_311 = "python3.11"
3✔
105
    PYTHON_312 = "python3.12"
3✔
106
    PYTHON_313 = "python3.13"
3✔
107

108
    def to_interpreter_version(self) -> tuple[int, int]:
3✔
109
        """Returns the Python version implied by the runtime, as (major, minor)."""
110
        mo = cast(Match, re.match(PYTHON_RUNTIME_REGEX, self.value))
3✔
111
        return int(mo.group("major")), int(mo.group("minor"))
3✔
112

113

114
LAMBDA_DOCKER_REPO = "public.ecr.aws/lambda/python"
3✔
115

116

117
class PythonAwsLambdaRuntime(PythonFaaSRuntimeField):
3✔
118
    # https://gallery.ecr.aws/lambda/python
119
    RUNTIME_TAG_MAPPING = {
3✔
120
        (PythonAwsLambdaFunctionRuntimes.PYTHON_36, FaaSArchitecture.X86_64): "3.6",
121
        (PythonAwsLambdaFunctionRuntimes.PYTHON_37, FaaSArchitecture.X86_64): "3.7",
122
        (PythonAwsLambdaFunctionRuntimes.PYTHON_38, FaaSArchitecture.X86_64): "3.8-x86_64",
123
        (PythonAwsLambdaFunctionRuntimes.PYTHON_38, FaaSArchitecture.ARM64): "3.8-arm64",
124
        (PythonAwsLambdaFunctionRuntimes.PYTHON_39, FaaSArchitecture.X86_64): "3.9-x86_64",
125
        (PythonAwsLambdaFunctionRuntimes.PYTHON_39, FaaSArchitecture.ARM64): "3.9-arm64",
126
        (PythonAwsLambdaFunctionRuntimes.PYTHON_310, FaaSArchitecture.X86_64): "3.10-x86_64",
127
        (PythonAwsLambdaFunctionRuntimes.PYTHON_310, FaaSArchitecture.ARM64): "3.10-arm64",
128
        (PythonAwsLambdaFunctionRuntimes.PYTHON_311, FaaSArchitecture.X86_64): "3.11-x86_64",
129
        (PythonAwsLambdaFunctionRuntimes.PYTHON_311, FaaSArchitecture.ARM64): "3.11-arm64",
130
        (PythonAwsLambdaFunctionRuntimes.PYTHON_312, FaaSArchitecture.X86_64): "3.12-x86_64",
131
        (PythonAwsLambdaFunctionRuntimes.PYTHON_312, FaaSArchitecture.ARM64): "3.12-arm64",
132
        (PythonAwsLambdaFunctionRuntimes.PYTHON_313, FaaSArchitecture.X86_64): "3.13-x86_64",
133
        (PythonAwsLambdaFunctionRuntimes.PYTHON_313, FaaSArchitecture.ARM64): "3.13-arm64",
134
    }
135

136
    help = help_text(
3✔
137
        """
138
        The identifier of the AWS Lambda runtime to target (pythonX.Y).
139
        See https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html.
140

141
        N.B.: only one of this and `complete_platforms` can be set. If `runtime` is set, a default complete
142
        platform is chosen, if one is known for that runtime. If you have issues either
143
        packaging the AWS Lambda PEX or running it as a deployed AWS Lambda function, you should try
144
        using an explicit `complete_platforms` instead.
145
        """
146
    )
147

148
    valid_choices = PythonAwsLambdaFunctionRuntimes
3✔
149
    known_runtimes = tuple(
3✔
150
        PythonFaaSKnownRuntime(
151
            runtime.value, *runtime.to_interpreter_version(), LAMBDA_DOCKER_REPO, tag, architecture
152
        )
153
        for (runtime, architecture), tag in RUNTIME_TAG_MAPPING.items()
154
    )
155

156
    @classmethod
3✔
157
    def compute_value(cls, raw_value: str | None, address: Address) -> str | None:
3✔
UNCOV
158
        value = super().compute_value(raw_value, address)
1✔
UNCOV
159
        if value is None:
1✔
160
            return None
×
UNCOV
161
        if not re.match(PYTHON_RUNTIME_REGEX, value):
1✔
162
            raise InvalidFieldException(
×
163
                softwrap(
164
                    f"""
165
                    The `{cls.alias}` field in target at {address} must be of the form pythonX.Y,
166
                    but was {value}.
167
                    """
168
                )
169
            )
UNCOV
170
        return value
1✔
171

172
    def to_interpreter_version(self) -> tuple[int, int] | None:
3✔
173
        """Returns the Python version implied by the runtime, as (major, minor)."""
UNCOV
174
        if self.value is None:
1✔
175
            return None
×
UNCOV
176
        mo = cast(Match, re.match(PYTHON_RUNTIME_REGEX, self.value))
1✔
UNCOV
177
        return int(mo.group("major")), int(mo.group("minor"))
1✔
178

179
    @classmethod
3✔
180
    def from_interpreter_version(cls, py_major: int, py_minor: int) -> str:
3✔
181
        return f"python{py_major}.{py_minor}"
×
182

183

184
class AWSLambdaArchitectureField(StringField):
3✔
185
    alias = "architecture"
3✔
186
    valid_choices = FaaSArchitecture
3✔
187
    expected_type = str
3✔
188
    default = FaaSArchitecture.X86_64.value
3✔
189
    help = help_text(
3✔
190
        """
191
        The architecture of the AWS Lambda runtime to target (x86_64 or arm64).
192
        See https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html.
193
        """
194
    )
195

196

197
class PythonAwsLambdaLayerDependenciesField(PythonFaaSDependencies):
3✔
198
    required = True
3✔
199

200

201
class _AWSLambdaBaseTarget(Target):
3✔
202
    core_fields: ClassVar[tuple[type[Field], ...]] = (
3✔
203
        *COMMON_TARGET_FIELDS,
204
        OutputPathField,
205
        PythonAwsLambdaIncludeRequirements,
206
        PythonAwsLambdaRuntime,
207
        PythonFaaSCompletePlatforms,
208
        PythonFaaSPex3VenvCreateExtraArgsField,
209
        PythonFaaSPexBuildExtraArgs,
210
        PythonFaaSLayoutField,
211
        PythonResolveField,
212
        EnvironmentField,
213
    )
214

215
    def validate(self) -> None:
3✔
216
        has_runtime = self[PythonAwsLambdaRuntime].value is not None
×
217
        has_complete_platforms = self[PexCompletePlatformsField].value is not None
×
218

219
        runtime_alias = self[PythonAwsLambdaRuntime].alias
×
220
        complete_platforms_alias = self[PexCompletePlatformsField].alias
×
221

222
        if has_runtime and has_complete_platforms:
×
223
            raise ValueError(
×
224
                softwrap(
225
                    f"""
226
                    The `{complete_platforms_alias}` takes precedence over the `{runtime_alias}` field, if
227
                    it is set. Remove the `{runtime_alias}` field to only use the `{complete_platforms_alias}`
228
                    value, or remove the `{complete_platforms_alias}` field to use the default platform
229
                    implied by `{runtime_alias}`.
230
                    """
231
                )
232
            )
233

234

235
class PythonAWSLambda(_AWSLambdaBaseTarget):
3✔
236
    alias = "python_aws_lambda_function"
3✔
237

238
    core_fields = (
3✔
239
        *_AWSLambdaBaseTarget.core_fields,
240
        PythonFaaSDependencies,
241
        PythonAwsLambdaHandlerField,
242
        AWSLambdaArchitectureField,
243
    )
244
    help = help_text(
3✔
245
        f"""
246
        A self-contained Python function suitable for uploading to AWS Lambda.
247

248
        See {doc_url("docs/python/integrations/aws-lambda")}.
249
        """
250
    )
251

252

253
class PythonAWSLambdaLayer(_AWSLambdaBaseTarget):
3✔
254
    alias = "python_aws_lambda_layer"
3✔
255
    core_fields = (
3✔
256
        *_AWSLambdaBaseTarget.core_fields,
257
        PythonAwsLambdaIncludeSources,
258
        PythonAwsLambdaLayerDependenciesField,
259
        AWSLambdaArchitectureField,
260
    )
261
    help = help_text(
3✔
262
        f"""
263
        A Python layer suitable for uploading to AWS Lambda.
264

265
        See {doc_url("docs/python/integrations/aws-lambda")}.
266
        """
267
    )
268

269

270
def rules():
3✔
271
    return (
2✔
272
        *collect_rules(),
273
        *faas_rules(),
274
    )
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