1# -*- coding: utf-8 -*-
2# Copyright 2026 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import sys
17
18import google.api_core as api_core
19
20from google.cloud.iam_credentials_v1 import gapic_version as package_version
21
22__version__ = package_version.__version__
23
24from importlib import metadata
25
26from .services.iam_credentials import IAMCredentialsAsyncClient, IAMCredentialsClient
27from .types.common import (
28 GenerateAccessTokenRequest,
29 GenerateAccessTokenResponse,
30 GenerateIdTokenRequest,
31 GenerateIdTokenResponse,
32 SignBlobRequest,
33 SignBlobResponse,
34 SignJwtRequest,
35 SignJwtResponse,
36)
37
38if hasattr(api_core, "check_python_version") and hasattr(
39 api_core, "check_dependency_versions"
40): # pragma: NO COVER
41 api_core.check_python_version("google.cloud.iam_credentials_v1") # type: ignore
42 api_core.check_dependency_versions("google.cloud.iam_credentials_v1") # type: ignore
43else: # pragma: NO COVER
44 # An older version of api_core is installed which does not define the
45 # functions above. We do equivalent checks manually.
46 try:
47 import warnings
48
49 _py_version_str = sys.version.split()[0]
50 _package_label = "google.cloud.iam_credentials_v1"
51 if sys.version_info < (3, 10):
52 warnings.warn(
53 "You are using a non-supported Python version "
54 + f"({_py_version_str}). Google will not post any further "
55 + f"updates to {_package_label} supporting this Python version. "
56 + "Please upgrade to the latest Python version, or at "
57 + f"least to Python 3.10, and then update {_package_label}.",
58 FutureWarning,
59 )
60
61 def parse_version_to_tuple(version_string: str):
62 """Safely converts a semantic version string to a comparable tuple of integers.
63 Example: "4.25.8" -> (4, 25, 8)
64 Ignores non-numeric parts and handles common version formats.
65 Args:
66 version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
67 Returns:
68 Tuple of integers for the parsed version string.
69 """
70 parts = []
71 for part in version_string.split("."):
72 try:
73 parts.append(int(part))
74 except ValueError:
75 # If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
76 # This is a simplification compared to 'packaging.parse_version', but sufficient
77 # for comparing strictly numeric semantic versions.
78 break
79 return tuple(parts)
80
81 def _get_version(dependency_name):
82 try:
83 version_string: str = metadata.version(dependency_name)
84 parsed_version = parse_version_to_tuple(version_string)
85 return (parsed_version, version_string)
86 except Exception:
87 # Catch exceptions from metadata.version() (e.g., PackageNotFoundError)
88 # or errors during parse_version_to_tuple
89 return (None, "--")
90
91 _dependency_package = "google.protobuf"
92 _next_supported_version = "4.25.8"
93 _next_supported_version_tuple = (4, 25, 8)
94 _recommendation = " (we recommend 6.x)"
95 (_version_used, _version_used_string) = _get_version(_dependency_package)
96 if _version_used and _version_used < _next_supported_version_tuple:
97 warnings.warn(
98 f"Package {_package_label} depends on "
99 + f"{_dependency_package}, currently installed at version "
100 + f"{_version_used_string}. Future updates to "
101 + f"{_package_label} will require {_dependency_package} at "
102 + f"version {_next_supported_version} or higher{_recommendation}."
103 + " Please ensure "
104 + "that either (a) your Python environment doesn't pin the "
105 + f"version of {_dependency_package}, so that updates to "
106 + f"{_package_label} can require the higher version, or "
107 + "(b) you manually update your Python environment to use at "
108 + f"least version {_next_supported_version} of "
109 + f"{_dependency_package}.",
110 FutureWarning,
111 )
112 except Exception:
113 warnings.warn(
114 "Could not determine the version of Python "
115 + "currently being used. To continue receiving "
116 + "updates for {_package_label}, ensure you are "
117 + "using a supported version of Python; see "
118 + "https://devguide.python.org/versions/"
119 )
120
121__all__ = (
122 "IAMCredentialsAsyncClient",
123 "GenerateAccessTokenRequest",
124 "GenerateAccessTokenResponse",
125 "GenerateIdTokenRequest",
126 "GenerateIdTokenResponse",
127 "IAMCredentialsClient",
128 "SignBlobRequest",
129 "SignBlobResponse",
130 "SignJwtRequest",
131 "SignJwtResponse",
132)