|
15 | 15 | # |
16 | 16 | from google.cloud.asset_v1 import gapic_version as package_version |
17 | 17 |
|
18 | | -__version__ = package_version.__version__ |
19 | | - |
20 | | - |
21 | 18 | import google.api_core as api_core |
| 19 | +import sys |
22 | 20 |
|
23 | | -if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER |
24 | | - api_core.check_python_version("google.cloud.asset_v1") # type: ignore |
25 | | - api_core.check_dependency_versions("google.cloud.asset_v1") # type: ignore |
26 | | -else: # pragma: NO COVER |
27 | | - # An older version of api_core is installed which does not define the |
28 | | - # functions above. We do equivalent checks manually. |
29 | | - try: |
30 | | - import warnings |
31 | | - import sys |
32 | | - |
33 | | - _py_version_str = sys.version.split()[0] |
34 | | - _package_label = "google.cloud.asset_v1" |
35 | | - if sys.version_info < (3, 9): |
36 | | - warnings.warn("You are using a non-supported Python version " + |
37 | | - f"({_py_version_str}). Google will not post any further " + |
38 | | - f"updates to {_package_label} supporting this Python version. " + |
39 | | - "Please upgrade to the latest Python version, or at " + |
40 | | - f"least to Python 3.9, and then update {_package_label}.", |
41 | | - FutureWarning) |
42 | | - if sys.version_info[:2] == (3, 9): |
43 | | - warnings.warn(f"You are using a Python version ({_py_version_str}) " + |
44 | | - f"which Google will stop supporting in {_package_label} in " + |
45 | | - "January 2026. Please " + |
46 | | - "upgrade to the latest Python version, or at " + |
47 | | - "least to Python 3.10, before then, and " + |
48 | | - f"then update {_package_label}.", |
49 | | - FutureWarning) |
50 | | - |
51 | | - from packaging.version import parse as parse_version |
52 | | - |
53 | | - if sys.version_info < (3, 8): |
54 | | - import pkg_resources |
55 | | - |
56 | | - def _get_version(dependency_name): |
57 | | - try: |
58 | | - version_string = pkg_resources.get_distribution(dependency_name).version |
59 | | - return (parse_version(version_string), version_string) |
60 | | - except pkg_resources.DistributionNotFound: |
61 | | - return (None, "--") |
62 | | - else: |
63 | | - from importlib import metadata |
64 | | - |
65 | | - def _get_version(dependency_name): |
66 | | - try: |
67 | | - version_string = metadata.version("requests") |
68 | | - parsed_version = parse_version(version_string) |
69 | | - return (parsed_version.release, version_string) |
70 | | - except metadata.PackageNotFoundError: |
71 | | - return (None, "--") |
| 21 | +__version__ = package_version.__version__ |
72 | 22 |
|
73 | | - _dependency_package = "google.protobuf" |
74 | | - _next_supported_version = "4.25.8" |
75 | | - _next_supported_version_tuple = (4, 25, 8) |
76 | | - _recommendation = " (we recommend 6.x)" |
77 | | - (_version_used, _version_used_string) = _get_version(_dependency_package) |
78 | | - if _version_used and _version_used < _next_supported_version_tuple: |
79 | | - warnings.warn(f"Package {_package_label} depends on " + |
80 | | - f"{_dependency_package}, currently installed at version " + |
81 | | - f"{_version_used_string}. Future updates to " + |
82 | | - f"{_package_label} will require {_dependency_package} at " + |
83 | | - f"version {_next_supported_version} or higher{_recommendation}." + |
84 | | - " Please ensure " + |
85 | | - "that either (a) your Python environment doesn't pin the " + |
86 | | - f"version of {_dependency_package}, so that updates to " + |
87 | | - f"{_package_label} can require the higher version, or " + |
88 | | - "(b) you manually update your Python environment to use at " + |
89 | | - f"least version {_next_supported_version} of " + |
90 | | - f"{_dependency_package}.", |
91 | | - FutureWarning) |
92 | | - except Exception: |
93 | | - warnings.warn("Could not determine the version of Python " + |
94 | | - "currently being used. To continue receiving " + |
95 | | - "updates for {_package_label}, ensure you are " + |
96 | | - "using a supported version of Python; see " + |
97 | | - "https://devguide.python.org/versions/") |
| 23 | +if sys.version_info >= (3, 8): # pragma: NO COVER |
| 24 | + from importlib import metadata |
| 25 | +else: # pragma: NO COVER |
| 26 | + # TODO(https://github.com/googleapis/python-api-core/issues/835): Remove |
| 27 | + # this code path once we drop support for Python 3.7 |
| 28 | + import importlib_metadata as metadata |
98 | 29 |
|
99 | 30 |
|
100 | 31 | from .services.asset_service import AssetServiceClient |
@@ -178,6 +109,90 @@ def _get_version(dependency_name): |
178 | 109 | from .types.assets import TimeWindow |
179 | 110 | from .types.assets import VersionedResource |
180 | 111 |
|
| 112 | +if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER |
| 113 | + api_core.check_python_version("google.cloud.asset_v1") # type: ignore |
| 114 | + api_core.check_dependency_versions("google.cloud.asset_v1") # type: ignore |
| 115 | +else: # pragma: NO COVER |
| 116 | + # An older version of api_core is installed which does not define the |
| 117 | + # functions above. We do equivalent checks manually. |
| 118 | + try: |
| 119 | + import warnings |
| 120 | + import sys |
| 121 | + |
| 122 | + _py_version_str = sys.version.split()[0] |
| 123 | + _package_label = "google.cloud.asset_v1" |
| 124 | + if sys.version_info < (3, 9): |
| 125 | + warnings.warn("You are using a non-supported Python version " + |
| 126 | + f"({_py_version_str}). Google will not post any further " + |
| 127 | + f"updates to {_package_label} supporting this Python version. " + |
| 128 | + "Please upgrade to the latest Python version, or at " + |
| 129 | + f"least to Python 3.9, and then update {_package_label}.", |
| 130 | + FutureWarning) |
| 131 | + if sys.version_info[:2] == (3, 9): |
| 132 | + warnings.warn(f"You are using a Python version ({_py_version_str}) " + |
| 133 | + f"which Google will stop supporting in {_package_label} in " + |
| 134 | + "January 2026. Please " + |
| 135 | + "upgrade to the latest Python version, or at " + |
| 136 | + "least to Python 3.10, before then, and " + |
| 137 | + f"then update {_package_label}.", |
| 138 | + FutureWarning) |
| 139 | + |
| 140 | + def parse_version_to_tuple(version_string: str): |
| 141 | + """Safely converts a semantic version string to a comparable tuple of integers. |
| 142 | + Example: "4.25.8" -> (4, 25, 8) |
| 143 | + Ignores non-numeric parts and handles common version formats. |
| 144 | + Args: |
| 145 | + version_string: Version string in the format "x.y.z" or "x.y.z<suffix>" |
| 146 | + Returns: |
| 147 | + Tuple of integers for the parsed version string. |
| 148 | + """ |
| 149 | + parts = [] |
| 150 | + for part in version_string.split("."): |
| 151 | + try: |
| 152 | + parts.append(int(part)) |
| 153 | + except ValueError: |
| 154 | + # If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here. |
| 155 | + # This is a simplification compared to 'packaging.parse_version', but sufficient |
| 156 | + # for comparing strictly numeric semantic versions. |
| 157 | + break |
| 158 | + return tuple(parts) |
| 159 | + |
| 160 | + def _get_version(dependency_name): |
| 161 | + try: |
| 162 | + version_string: str = metadata.version(dependency_name) |
| 163 | + parsed_version = parse_version_to_tuple(version_string) |
| 164 | + return (parsed_version, version_string) |
| 165 | + except Exception: |
| 166 | + # Catch exceptions from metadata.version() (e.g., PackageNotFoundError) |
| 167 | + # or errors during parse_version_to_tuple |
| 168 | + return (None, "--") |
| 169 | + |
| 170 | + _dependency_package = "google.protobuf" |
| 171 | + _next_supported_version = "4.25.8" |
| 172 | + _next_supported_version_tuple = (4, 25, 8) |
| 173 | + _recommendation = " (we recommend 6.x)" |
| 174 | + (_version_used, _version_used_string) = _get_version(_dependency_package) |
| 175 | + if _version_used and _version_used < _next_supported_version_tuple: |
| 176 | + warnings.warn(f"Package {_package_label} depends on " + |
| 177 | + f"{_dependency_package}, currently installed at version " + |
| 178 | + f"{_version_used_string}. Future updates to " + |
| 179 | + f"{_package_label} will require {_dependency_package} at " + |
| 180 | + f"version {_next_supported_version} or higher{_recommendation}." + |
| 181 | + " Please ensure " + |
| 182 | + "that either (a) your Python environment doesn't pin the " + |
| 183 | + f"version of {_dependency_package}, so that updates to " + |
| 184 | + f"{_package_label} can require the higher version, or " + |
| 185 | + "(b) you manually update your Python environment to use at " + |
| 186 | + f"least version {_next_supported_version} of " + |
| 187 | + f"{_dependency_package}.", |
| 188 | + FutureWarning) |
| 189 | + except Exception: |
| 190 | + warnings.warn("Could not determine the version of Python " + |
| 191 | + "currently being used. To continue receiving " + |
| 192 | + "updates for {_package_label}, ensure you are " + |
| 193 | + "using a supported version of Python; see " + |
| 194 | + "https://devguide.python.org/versions/") |
| 195 | + |
181 | 196 | __all__ = ( |
182 | 197 | 'AssetServiceAsyncClient', |
183 | 198 | 'AnalyzeIamPolicyLongrunningMetadata', |
|
0 commit comments