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

pantsbuild / pants / 20332790708

18 Dec 2025 09:48AM UTC coverage: 64.992% (-15.3%) from 80.295%
20332790708

Pull #22949

github

web-flow
Merge f730a56cd into 407284c67
Pull Request #22949: Add experimental uv resolver for Python lockfiles

54 of 97 new or added lines in 5 files covered. (55.67%)

8270 existing lines in 295 files now uncovered.

48990 of 75379 relevant lines covered (64.99%)

1.81 hits per line

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

0.0
/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

UNCOV
4
from __future__ import annotations
×
5

UNCOV
6
import re
×
UNCOV
7
from dataclasses import dataclass
×
UNCOV
8
from enum import Enum
×
UNCOV
9
from re import Match
×
UNCOV
10
from typing import ClassVar, cast
×
11

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

40

UNCOV
41
class PythonAwsLambdaHandlerField(PythonFaaSHandlerField):
×
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
UNCOV
45
    reexported_handler_module = "lambda_function"
×
46

UNCOV
47
    help = help_text(
×
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

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

65

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

70

UNCOV
71
class PythonAwsLambdaIncludeRequirements(BoolField):
×
UNCOV
72
    alias = "include_requirements"
×
UNCOV
73
    default = True
×
UNCOV
74
    help = help_text(
×
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

UNCOV
83
class PythonAwsLambdaIncludeSources(BoolField):
×
UNCOV
84
    alias = "include_sources"
×
UNCOV
85
    default = True
×
UNCOV
86
    help = help_text(
×
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

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

97

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

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

113

UNCOV
114
LAMBDA_DOCKER_REPO = "public.ecr.aws/lambda/python"
×
115

116

UNCOV
117
class PythonAwsLambdaRuntime(PythonFaaSRuntimeField):
×
118
    # https://gallery.ecr.aws/lambda/python
UNCOV
119
    RUNTIME_TAG_MAPPING = {
×
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

UNCOV
136
    help = help_text(
×
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

UNCOV
148
    valid_choices = PythonAwsLambdaFunctionRuntimes
×
UNCOV
149
    known_runtimes = tuple(
×
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

UNCOV
156
    @classmethod
×
UNCOV
157
    def compute_value(cls, raw_value: str | None, address: Address) -> str | None:
×
UNCOV
158
        value = super().compute_value(raw_value, address)
×
UNCOV
159
        if value is None:
×
160
            return None
×
UNCOV
161
        if not re.match(PYTHON_RUNTIME_REGEX, value):
×
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
×
171

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

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

183

UNCOV
184
class AWSLambdaArchitectureField(StringField):
×
UNCOV
185
    alias = "architecture"
×
UNCOV
186
    valid_choices = FaaSArchitecture
×
UNCOV
187
    expected_type = str
×
UNCOV
188
    default = FaaSArchitecture.X86_64.value
×
UNCOV
189
    help = help_text(
×
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

UNCOV
197
class PythonAwsLambdaLayerDependenciesField(PythonFaaSDependencies):
×
UNCOV
198
    required = True
×
199

200

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

UNCOV
215
    def validate(self) -> None:
×
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

UNCOV
235
class PythonAWSLambda(_AWSLambdaBaseTarget):
×
UNCOV
236
    alias = "python_aws_lambda_function"
×
237

UNCOV
238
    core_fields = (
×
239
        *_AWSLambdaBaseTarget.core_fields,
240
        PythonFaaSDependencies,
241
        PythonAwsLambdaHandlerField,
242
        AWSLambdaArchitectureField,
243
    )
UNCOV
244
    help = help_text(
×
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

UNCOV
253
class PythonAWSLambdaLayer(_AWSLambdaBaseTarget):
×
UNCOV
254
    alias = "python_aws_lambda_layer"
×
UNCOV
255
    core_fields = (
×
256
        *_AWSLambdaBaseTarget.core_fields,
257
        PythonAwsLambdaIncludeSources,
258
        PythonAwsLambdaLayerDependenciesField,
259
        AWSLambdaArchitectureField,
260
    )
UNCOV
261
    help = help_text(
×
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

UNCOV
270
def rules():
×
UNCOV
271
    return (
×
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