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

pantsbuild / pants / 25441711719

06 May 2026 02:31PM UTC coverage: 92.915%. Remained the same
25441711719

push

github

web-flow
use sha pin (with comment) format for generated actions (#23312)

Per the GitHub Action best practices we recently enabled at #23249, we
should pin each action to a SHA so that the reference is actually
immutable.

This will -- I hope -- knock out a large chunk of the 421 alerts we
currently get from zizmor. The next followup would then be upgrades and
harmonizing the generated and none-generated pins.

Notice: This idea was suggested by Claude while going over pinact output
and I was surprised to see that post processing the yaml wasn't too
gross.

92206 of 99237 relevant lines covered (92.91%)

4.04 hits per line

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

97.14
/src/python/pants/backend/google_cloud_function/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
import re
2✔
5
from enum import Enum
2✔
6
from re import Match
2✔
7
from typing import cast
2✔
8

9
from pants.backend.python.target_types import PexCompletePlatformsField, PythonResolveField
2✔
10
from pants.backend.python.util_rules.faas import (
2✔
11
    FaaSArchitecture,
12
    PythonFaaSCompletePlatforms,
13
    PythonFaaSDependencies,
14
    PythonFaaSHandlerField,
15
    PythonFaaSKnownRuntime,
16
    PythonFaaSLayoutField,
17
    PythonFaaSPex3VenvCreateExtraArgsField,
18
    PythonFaaSPexBuildExtraArgs,
19
    PythonFaaSRuntimeField,
20
)
21
from pants.backend.python.util_rules.faas import rules as faas_rules
2✔
22
from pants.core.environments.target_types import EnvironmentField
2✔
23
from pants.core.goals.package import OutputPathField
2✔
24
from pants.engine.addresses import Address
2✔
25
from pants.engine.rules import collect_rules
2✔
26
from pants.engine.target import COMMON_TARGET_FIELDS, InvalidFieldException, StringField, Target
2✔
27
from pants.util.docutil import doc_url
2✔
28
from pants.util.strutil import help_text, softwrap
2✔
29

30

31
class PythonGoogleCloudFunctionHandlerField(PythonFaaSHandlerField):
2✔
32
    # GCP requires "Your main file must be named main.py"
33
    # https://cloud.google.com/functions/docs/writing#directory-structure-python
34
    reexported_handler_module = "main"
2✔
35

36
    help = help_text(
2✔
37
        f"""
38
        Entry point to the Google Cloud Function handler.
39

40
        {PythonFaaSHandlerField.help}
41

42
        This is re-exported at `{reexported_handler_module}.handler` in the resulting package to
43
        used as the configured handler of the Google Cloud Function in GCP.  It can also be accessed
44
        under its source-root-relative module path, for example: `path.to.module.handler_func`.
45
        """
46
    )
47

48

49
PYTHON_RUNTIME_REGEX = r"^python(?P<major>\d)(?P<minor>\d+)$"
2✔
50

51

52
class PythonGoogleCloudFunctionRuntimes(Enum):
2✔
53
    PYTHON_37 = "python37"
2✔
54
    PYTHON_38 = "python38"
2✔
55
    PYTHON_39 = "python39"
2✔
56
    PYTHON_310 = "python310"
2✔
57
    PYTHON_311 = "python311"
2✔
58
    PYTHON_312 = "python312"
2✔
59

60
    def to_interpreter_version(self) -> tuple[int, int]:
2✔
61
        """Returns the Python version implied by the runtime, as (major, minor)."""
62
        mo = cast(Match, re.match(PYTHON_RUNTIME_REGEX, self.value))
2✔
63
        return int(mo.group("major")), int(mo.group("minor"))
2✔
64

65

66
class PythonGoogleCloudFunctionRuntime(PythonFaaSRuntimeField):
2✔
67
    DOCKER_RUNTIME_MAPPING = {
2✔
68
        PythonGoogleCloudFunctionRuntimes.PYTHON_37: (
69
            "us-central1-docker.pkg.dev/serverless-runtimes/google-18-full/runtimes/python37",
70
            "python37_20240728_3_7_17_RC00",
71
        ),
72
        PythonGoogleCloudFunctionRuntimes.PYTHON_38: (
73
            "us-central1-docker.pkg.dev/serverless-runtimes/google-18-full/runtimes/python38",
74
            "python38_20240728_3_8_19_RC00",
75
        ),
76
        PythonGoogleCloudFunctionRuntimes.PYTHON_39: (
77
            "us-central1-docker.pkg.dev/serverless-runtimes/google-18-full/runtimes/python39",
78
            "python39_20240728_3_9_19_RC00",
79
        ),
80
        PythonGoogleCloudFunctionRuntimes.PYTHON_310: (
81
            "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/python310",
82
            "python310_20240728_3_10_14_RC00",
83
        ),
84
        PythonGoogleCloudFunctionRuntimes.PYTHON_311: (
85
            "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/python311",
86
            "python311_20240728_3_11_9_RC00",
87
        ),
88
        PythonGoogleCloudFunctionRuntimes.PYTHON_312: (
89
            "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/python312",
90
            "python312_20240728_3_12_4_RC00",
91
        ),
92
    }
93

94
    valid_choices = PythonGoogleCloudFunctionRuntimes
2✔
95
    help = help_text(
2✔
96
        """
97
        The identifier of the Google Cloud Function runtime to target (pythonXY). See
98
        https://cloud.google.com/functions/docs/concepts/python-runtime.
99

100
        In general you'll want to define either a `runtime` or one `complete_platforms` but not
101
        both. Specifying a `runtime` is simpler, but less accurate. If you have issues either
102
        packaging the Google Cloud Function PEX or running it as a deployed Google Cloud Function,
103
        you should try using `complete_platforms` instead.
104
        """
105
    )
106

107
    known_runtimes = tuple(
2✔
108
        PythonFaaSKnownRuntime(
109
            runtime.value,
110
            *runtime.to_interpreter_version(),
111
            docker_repo,
112
            docker_tag,
113
            FaaSArchitecture.X86_64,
114
        )
115
        for runtime, (docker_repo, docker_tag) in DOCKER_RUNTIME_MAPPING.items()
116
    )
117

118
    @classmethod
2✔
119
    def compute_value(cls, raw_value: str | None, address: Address) -> str | None:
2✔
120
        value = super().compute_value(raw_value, address)
2✔
121
        if value is None:
2✔
122
            return None
1✔
123
        if not re.match(PYTHON_RUNTIME_REGEX, value):
2✔
124
            raise InvalidFieldException(
×
125
                f"The `{cls.alias}` field in target at {address} must be of the form pythonXY, "
126
                f"but was {value}."
127
            )
128
        return value
2✔
129

130
    def to_interpreter_version(self) -> tuple[int, int] | None:
2✔
131
        """Returns the Python version implied by the runtime, as (major, minor)."""
132
        if self.value is None:
2✔
133
            return None
1✔
134
        mo = cast(Match, re.match(PYTHON_RUNTIME_REGEX, self.value))
2✔
135
        return int(mo.group("major")), int(mo.group("minor"))
2✔
136

137
    @classmethod
2✔
138
    def from_interpreter_version(cls, py_major: int, py_minor) -> str:
2✔
139
        return f"python{py_major}{py_minor}"
1✔
140

141

142
class GoogleCloudFunctionTypes(Enum):
2✔
143
    EVENT = "event"
2✔
144
    HTTP = "http"
2✔
145

146

147
class PythonGoogleCloudFunctionType(StringField):
2✔
148
    alias = "type"
2✔
149
    required = True
2✔
150
    valid_choices = GoogleCloudFunctionTypes
2✔
151
    help = help_text(
2✔
152
        """
153
        The trigger type of the cloud function. Can either be `'event'` or `'http'`.
154
        See https://cloud.google.com/functions/docs/concepts/python-runtime for reference to
155
        `--trigger-http`.
156
        """
157
    )
158

159

160
class PythonGoogleCloudFunction(Target):
2✔
161
    alias = "python_google_cloud_function"
2✔
162
    core_fields = (
2✔
163
        *COMMON_TARGET_FIELDS,
164
        OutputPathField,
165
        PythonFaaSDependencies,
166
        PythonGoogleCloudFunctionHandlerField,
167
        PythonGoogleCloudFunctionRuntime,
168
        PythonFaaSCompletePlatforms,
169
        PythonGoogleCloudFunctionType,
170
        PythonFaaSPex3VenvCreateExtraArgsField,
171
        PythonFaaSPexBuildExtraArgs,
172
        PythonFaaSLayoutField,
173
        PythonResolveField,
174
        EnvironmentField,
175
    )
176
    help = help_text(
2✔
177
        f"""
178
        A self-contained Python function suitable for uploading to Google Cloud Function.
179

180
        See {doc_url("docs/python/integrations/google-cloud-functions")}.
181
        """
182
    )
183

184
    def validate(self) -> None:
2✔
185
        has_runtime = self[PythonGoogleCloudFunctionRuntime].value is not None
1✔
186
        has_complete_platforms = self[PexCompletePlatformsField].value is not None
1✔
187

188
        runtime_alias = self[PythonGoogleCloudFunctionRuntime].alias
1✔
189
        complete_platforms_alias = self[PexCompletePlatformsField].alias
1✔
190

191
        if has_runtime and has_complete_platforms:
1✔
192
            raise ValueError(
×
193
                softwrap(
194
                    f"""
195
                    The `{complete_platforms_alias}` takes precedence over the `{runtime_alias}` field, if
196
                    it is set. Remove the `{runtime_alias}` field to only use the `{complete_platforms_alias}`
197
                    value, or remove the `{complete_platforms_alias}` field to use the default platform
198
                    implied by `{runtime_alias}`.
199
                    """
200
                ),
201
            )
202

203

204
def rules():
2✔
205
    return (
1✔
206
        *collect_rules(),
207
        *faas_rules(),
208
    )
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