Skip to content

Commit 35e3314

Browse files
committed
allow Python, dependency versions checks without upgrading api_core
The preferred method is still to sue the code in api_core, but we have a (repetitive) fallback in case users haven't installed the new version that implements that functionality.
1 parent 235e5b8 commit 35e3314

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,61 @@ __version__ = package_version.__version__
1010

1111
import google.api_core as api_core
1212

13-
{# How do we get the name of the PyPI path into this template? We
14-
want the string arguments below to be something like
15-
"google-cloud-foo (google.cloud.foo)", where the name outside the
16-
parentheses is the PyPI package name, and the the name inside the
17-
parentheses is the qualified Python package name installed. #}
18-
api_core.check_python_version("{{package_path}}")
19-
api_core.check_dependency_versions("{{package_path}}")
13+
try:
14+
api_core.check_python_version("{{package_path}}")
15+
api_core.check_dependency_versions("{{package_path}}")
16+
except AttributeError:
17+
{# TODO: Remove this try-catch when we require api-core at a version that
18+
supports the changes in https://github.com/googleapis/python-api-core/pull/832
2019
20+
In the meantime, please ensure the functionality here mirrors the
21+
equivalent functionality in api_core, in those two functions above.
22+
-#}
23+
# An older version of api_core is installed, which does not define the
24+
# functions above. We do equivalent checks manually.
25+
26+
import logging
27+
28+
_py_version_str = sys.version.split()[0]
29+
_package_label = "{{package_path}}"
30+
if sys.version_info < (3, 9):
31+
logging.warning("You are using a non-supported Python version " +
32+
f"({_py_version_str}). Google will not post any further " +
33+
f"updates to {_package_label} supporting this Python version. " +
34+
"Please upgrade to the latest Python version, or at " +
35+
f"least to Python 3.9, and then update {_package_label}.")
36+
if sys.version_info[:2] == (3, 9):
37+
logging.warning(f"You are using a Python version ({_py_version_str}) " +
38+
f"which Google will stop supporting in {_package_label} when " +
39+
"it reaches its end of life (October 2025). Please " +
40+
"upgrade to the latest Python version, or at " +
41+
"least Python 3.10, before then, and " +
42+
f"then update {_package_label}. "
43+
44+
import pkg_resources
45+
from packaging.version import parse as parse_version
46+
47+
def _get_version(dependency_name):
48+
version_string = pkg_resources.get_distribution(dependency_name).version
49+
return parse_version(version_string)
50+
51+
_dependency_package = "google.protobuf"
52+
_version_used = _get_version(_dependency_package)
53+
_next_supported_version = "4.25.8"
54+
if _version_used < parse_version(_next_supported_version):
55+
logging.warning(f"DEPRECATION: Package {_package_label} depends on " +
56+
f"{_dependency_package}, currently installed at version " +
57+
f"{_version_used.__str__}. Future updates to " +
58+
f"{_package_label} will require {_dependency_package} at " +
59+
f"version {_next_supported_version} or higher. Please ensure " +
60+
"that either (a) your Python environment doesn't pin the " +
61+
f"version of {_dependency_package}, so that updates to " +
62+
f"{_package_label} can require the higher version, or " +
63+
"(b) you manually update your Python environment to use at " +
64+
f"least version {_next_supported_version} of " +
65+
f"{_dependency_package}."
66+
67+
2168
{# Import subpackages. -#}
2269
{% for subpackage, _ in api.subpackages|dictsort %}
2370
from . import {{ subpackage }}

0 commit comments

Comments
 (0)