Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 5f4bb03

Browse files
committed
feat: add try block around version check
1 parent 406f48d commit 5f4bb03

1 file changed

Lines changed: 69 additions & 63 deletions

File tree

gapic/templates/%namespace/%name_%version/%sub/__init__.py.j2

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,71 +21,77 @@ else: # pragma: NO COVER
2121
In the meantime, please ensure the functionality here mirrors the
2222
equivalent functionality in api_core, in those two functions above.
2323
#}
24-
# An older version of api_core is installed, which does not define the
24+
# An older version of api_core is installed which does not define the
2525
# functions above. We do equivalent checks manually.
26-
27-
import warnings
28-
import sys
29-
30-
_py_version_str = sys.version.split()[0]
31-
_package_label = "{{package_path}}"
32-
if sys.version_info < (3, 9):
33-
warnings.warn("You are using a non-supported Python version " +
34-
f"({_py_version_str}). Google will not post any further " +
35-
f"updates to {_package_label} supporting this Python version. " +
36-
"Please upgrade to the latest Python version, or at " +
37-
f"least to Python 3.9, and then update {_package_label}.",
38-
FutureWarning)
39-
if sys.version_info[:2] == (3, 9):
40-
warnings.warn(f"You are using a Python version ({_py_version_str}) " +
41-
f"which Google will stop supporting in {_package_label} when " +
42-
"it reaches its end of life (October 2025). Please " +
43-
"upgrade to the latest Python version, or at " +
44-
"least Python 3.10, before then, and " +
45-
f"then update {_package_label}.",
46-
FutureWarning)
47-
48-
from packaging.version import parse as parse_version
49-
50-
if sys.version_info < (3, 8):
51-
import pkg_resources
52-
53-
def _get_version(dependency_name):
54-
try:
55-
version_string = pkg_resources.get_distribution(dependency_name).version
56-
return (parse_version(version_string), version_string)
57-
except pkg_resources.DistributionNotFound:
58-
return (None, "--")
59-
else:
60-
from importlib import metadata
61-
62-
def _get_version(dependency_name):
63-
try:
64-
version_string = metadata.version("requests")
65-
parsed_version = parse_version(version_string)
66-
return (parsed_version.release, version_string)
67-
except metadata.PackageNotFoundError:
26+
try:
27+
import warnings
28+
import sys
29+
30+
_py_version_str = sys.version.split()[0]
31+
_package_label = "{{package_path}}"
32+
if sys.version_info < (3, 9):
33+
warnings.warn("You are using a non-supported Python version " +
34+
f"({_py_version_str}). Google will not post any further " +
35+
f"updates to {_package_label} supporting this Python version. " +
36+
"Please upgrade to the latest Python version, or at " +
37+
f"least to Python 3.9, and then update {_package_label}.",
38+
FutureWarning)
39+
if sys.version_info[:2] == (3, 9):
40+
warnings.warn(f"You are using a Python version ({_py_version_str}) " +
41+
f"which Google will stop supporting in {_package_label} in " +
42+
"January 2026. Please " +
43+
"upgrade to the latest Python version, or at " +
44+
"least to Python 3.10, before then, and " +
45+
f"then update {_package_label}.",
46+
FutureWarning)
47+
48+
from packaging.version import parse as parse_version
49+
50+
if sys.version_info < (3, 8):
51+
import pkg_resources
52+
53+
def _get_version(dependency_name):
54+
try:
55+
version_string = pkg_resources.get_distribution(dependency_name).version
56+
return (parse_version(version_string), version_string)
57+
except pkg_resources.DistributionNotFound:
6858
return (None, "--")
69-
70-
_dependency_package = "google.protobuf"
71-
_next_supported_version = "4.25.8"
72-
_next_supported_version_tuple = (4, 25, 8)
73-
_recommendation = " (we recommend 6.x)"
74-
(_version_used, _version_used_string) = _get_version(_dependency_package)
75-
if _version_used and _version_used < _next_supported_version_tuple:
76-
warnings.warn(f"Package {_package_label} depends on " +
77-
f"{_dependency_package}, currently installed at version " +
78-
f"{_version_used_string}. Future updates to " +
79-
f"{_package_label} will require {_dependency_package} at " +
80-
f"version {_next_supported_version} or higher{_recommendation}." +
81-
" Please ensure " +
82-
"that either (a) your Python environment doesn't pin the " +
83-
f"version of {_dependency_package}, so that updates to " +
84-
f"{_package_label} can require the higher version, or " +
85-
"(b) you manually update your Python environment to use at " +
86-
f"least version {_next_supported_version} of " +
87-
f"{_dependency_package}.",
88-
FutureWarning)
59+
else:
60+
from importlib import metadata
61+
62+
def _get_version(dependency_name):
63+
try:
64+
version_string = metadata.version("requests")
65+
parsed_version = parse_version(version_string)
66+
return (parsed_version.release, version_string)
67+
except metadata.PackageNotFoundError:
68+
return (None, "--")
69+
70+
_dependency_package = "google.protobuf"
71+
_next_supported_version = "4.25.8"
72+
_next_supported_version_tuple = (4, 25, 8)
73+
_recommendation = " (we recommend 6.x)"
74+
(_version_used, _version_used_string) = _get_version(_dependency_package)
75+
if _version_used and _version_used < _next_supported_version_tuple:
76+
warnings.warn(f"Package {_package_label} depends on " +
77+
f"{_dependency_package}, currently installed at version " +
78+
f"{_version_used_string}. Future updates to " +
79+
f"{_package_label} will require {_dependency_package} at " +
80+
f"version {_next_supported_version} or higher{_recommendation}." +
81+
" Please ensure " +
82+
"that either (a) your Python environment doesn't pin the " +
83+
f"version of {_dependency_package}, so that updates to " +
84+
f"{_package_label} can require the higher version, or " +
85+
"(b) you manually update your Python environment to use at " +
86+
f"least version {_next_supported_version} of " +
87+
f"{_dependency_package}.",
88+
FutureWarning)
89+
except Exception:
90+
warnings.warn("Could not determine the version of Python " +
91+
"currently being used. To continue receiving " +
92+
"updates for {_package_label}, ensure you are " +
93+
"using a supported version of Python; see " +
94+
"https://devguide.python.org/versions/")
8995

9096
{# Import subpackages. -#}
9197
{% for subpackage, _ in api.subpackages|dictsort %}

0 commit comments

Comments
 (0)