Skip to content

Commit 95b1e7a

Browse files
committed
WIP: try to catch any init-time exceptions making the test fail
Not at all clear this is the underlying issue, but maybe
1 parent 96842e8 commit 95b1e7a

File tree

8 files changed

+517
-501
lines changed

8 files changed

+517
-501
lines changed

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

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,83 @@ __version__ = package_version.__version__
99

1010

1111
import google.api_core as api_core
12-
13-
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
14-
{# TODO(api_core): remove `type:ignore` below when minimum version of api_core makes the else clause unnecessary. #}
15-
api_core.check_python_version("{{package_path}}") # type: ignore
16-
api_core.check_dependency_versions("{{package_path}}") # type: ignore
17-
else: # pragma: NO COVER
18-
{# TODO(api_core): Remove this try-catch when we require api-core at a version that
19-
supports the changes in https://github.com/googleapis/python-api-core/pull/832
20-
21-
In the meantime, please ensure the functionality here mirrors the
22-
equivalent functionality in api_core, in those two functions above.
23-
#}
24-
# An older version of api_core is installed, which does not define the
25-
# 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:
12+
try:
13+
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
14+
{# TODO(api_core): remove `type:ignore` below when minimum version of api_core makes the else clause unnecessary. #}
15+
api_core.check_python_version("{{package_path}}") # type: ignore
16+
api_core.check_dependency_versions("{{package_path}}") # type: ignore
17+
else: # pragma: NO COVER
18+
{# TODO(api_core): Remove this try-catch when we require api-core at a version that
19+
supports the changes in https://github.com/googleapis/python-api-core/pull/832
20+
21+
In the meantime, please ensure the functionality here mirrors the
22+
equivalent functionality in api_core, in those two functions above.
23+
#}
24+
# An older version of api_core is installed, which does not define the
25+
# 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:
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-
(_version_used, _version_used_string) = _get_version(_dependency_package)
74-
if _version_used and _version_used < _next_supported_version_tuple:
75-
warnings.warn(f"Package {_package_label} depends on " +
76-
f"{_dependency_package}, currently installed at version " +
77-
f"{_version_used_string}. Future updates to " +
78-
f"{_package_label} will require {_dependency_package} at " +
79-
f"version {_next_supported_version} or higher. Please ensure " +
80-
"that either (a) your Python environment doesn't pin the " +
81-
f"version of {_dependency_package}, so that updates to " +
82-
f"{_package_label} can require the higher version, or " +
83-
"(b) you manually update your Python environment to use at " +
84-
f"least version {_next_supported_version} of " +
85-
f"{_dependency_package}.",
86-
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+
(_version_used, _version_used_string) = _get_version(_dependency_package)
74+
if _version_used and _version_used < _next_supported_version_tuple:
75+
warnings.warn(f"Package {_package_label} depends on " +
76+
f"{_dependency_package}, currently installed at version " +
77+
f"{_version_used_string}. Future updates to " +
78+
f"{_package_label} will require {_dependency_package} at " +
79+
f"version {_next_supported_version} or higher. Please ensure " +
80+
"that either (a) your Python environment doesn't pin the " +
81+
f"version of {_dependency_package}, so that updates to " +
82+
f"{_package_label} can require the higher version, or " +
83+
"(b) you manually update your Python environment to use at " +
84+
f"least version {_next_supported_version} of " +
85+
f"{_dependency_package}.",
86+
FutureWarning)
87+
except Exception as exception: # pragma: NO COVER
88+
warnings.warn(f"Unexpected exception: {exception}")
8789

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

tests/integration/goldens/asset/google/cloud/asset_v1/__init__.py

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,74 +19,76 @@
1919

2020

2121
import google.api_core as api_core
22+
try:
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.
2229

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.
30+
import warnings
31+
import sys
2932

30-
import warnings
31-
import sys
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} when " +
45+
"it reaches its end of life (October 2025). Please " +
46+
"upgrade to the latest Python version, or at " +
47+
"least Python 3.10, before then, and " +
48+
f"then update {_package_label}.",
49+
FutureWarning)
3250

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} when " +
45-
"it reaches its end of life (October 2025). Please " +
46-
"upgrade to the latest Python version, or at " +
47-
"least Python 3.10, before then, and " +
48-
f"then update {_package_label}.",
49-
FutureWarning)
51+
from packaging.version import parse as parse_version
5052

51-
from packaging.version import parse as parse_version
53+
if sys.version_info < (3, 8):
54+
import pkg_resources
5255

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:
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:
7161
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, "--")
7272

73-
_dependency_package = "google.protobuf"
74-
_next_supported_version = "4.25.8"
75-
_next_supported_version_tuple = (4, 25, 8)
76-
(_version_used, _version_used_string) = _get_version(_dependency_package)
77-
if _version_used and _version_used < _next_supported_version_tuple:
78-
warnings.warn(f"Package {_package_label} depends on " +
79-
f"{_dependency_package}, currently installed at version " +
80-
f"{_version_used_string}. Future updates to " +
81-
f"{_package_label} will require {_dependency_package} at " +
82-
f"version {_next_supported_version} or higher. Please ensure " +
83-
"that either (a) your Python environment doesn't pin the " +
84-
f"version of {_dependency_package}, so that updates to " +
85-
f"{_package_label} can require the higher version, or " +
86-
"(b) you manually update your Python environment to use at " +
87-
f"least version {_next_supported_version} of " +
88-
f"{_dependency_package}.",
89-
FutureWarning)
73+
_dependency_package = "google.protobuf"
74+
_next_supported_version = "4.25.8"
75+
_next_supported_version_tuple = (4, 25, 8)
76+
(_version_used, _version_used_string) = _get_version(_dependency_package)
77+
if _version_used and _version_used < _next_supported_version_tuple:
78+
warnings.warn(f"Package {_package_label} depends on " +
79+
f"{_dependency_package}, currently installed at version " +
80+
f"{_version_used_string}. Future updates to " +
81+
f"{_package_label} will require {_dependency_package} at " +
82+
f"version {_next_supported_version} or higher. Please ensure " +
83+
"that either (a) your Python environment doesn't pin the " +
84+
f"version of {_dependency_package}, so that updates to " +
85+
f"{_package_label} can require the higher version, or " +
86+
"(b) you manually update your Python environment to use at " +
87+
f"least version {_next_supported_version} of " +
88+
f"{_dependency_package}.",
89+
FutureWarning)
90+
except Exception as exception: # pragma: NO COVER
91+
warnings.warn(f"Unexpected exception: {exception}")
9092

9193

9294
from .services.asset_service import AssetServiceClient

0 commit comments

Comments
 (0)