Skip to content

Commit 0311b27

Browse files
committed
moved parse_version_to_tuple into else block
1 parent 356d178 commit 0311b27

File tree

7 files changed

+139
-140
lines changed

7 files changed

+139
-140
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@ from .types.{{ proto.module_name }} import {{ enum.name }}
4949

5050
ParsedVersion = Tuple[int, ...]
5151

52-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
53-
"""Safely converts a semantic version string to a comparable tuple of integers.
54-
Example: "4.25.8" -> (4, 25, 8)
55-
Ignores non-numeric parts and handles common version formats.
56-
Args:
57-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
58-
Returns:
59-
Tuple of integers for the parsed version string.
60-
"""
61-
parts = []
62-
for part in version_string.split("."):
63-
try:
64-
parts.append(int(part))
65-
except ValueError:
66-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
67-
# This is a simplification compared to 'packaging.parse_version', but sufficient
68-
# for comparing strictly numeric semantic versions.
69-
break
70-
return tuple(parts)
71-
7252
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
7353
{# TODO(api_core): remove `type:ignore` below when minimum version of api_core makes the else clause unnecessary. #}
7454
api_core.check_python_version("{{package_path}}") # type: ignore
@@ -104,6 +84,25 @@ else: # pragma: NO COVER
10484
f"then update {_package_label}.",
10585
FutureWarning)
10686

87+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
88+
"""Safely converts a semantic version string to a comparable tuple of integers.
89+
Example: "4.25.8" -> (4, 25, 8)
90+
Ignores non-numeric parts and handles common version formats.
91+
Args:
92+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
93+
Returns:
94+
Tuple of integers for the parsed version string.
95+
"""
96+
parts = []
97+
for part in version_string.split("."):
98+
try:
99+
parts.append(int(part))
100+
except ValueError:
101+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
102+
# This is a simplification compared to 'packaging.parse_version', but sufficient
103+
# for comparing strictly numeric semantic versions.
104+
break
105+
return tuple(parts)
107106

108107
def _get_version(dependency_name):
109108
try:

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,6 @@
112112

113113
ParsedVersion = Tuple[int, ...]
114114

115-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
116-
"""Safely converts a semantic version string to a comparable tuple of integers.
117-
Example: "4.25.8" -> (4, 25, 8)
118-
Ignores non-numeric parts and handles common version formats.
119-
Args:
120-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
121-
Returns:
122-
Tuple of integers for the parsed version string.
123-
"""
124-
parts = []
125-
for part in version_string.split("."):
126-
try:
127-
parts.append(int(part))
128-
except ValueError:
129-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
130-
# This is a simplification compared to 'packaging.parse_version', but sufficient
131-
# for comparing strictly numeric semantic versions.
132-
break
133-
return tuple(parts)
134-
135115
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
136116
api_core.check_python_version("google.cloud.asset_v1") # type: ignore
137117
api_core.check_dependency_versions("google.cloud.asset_v1") # type: ignore
@@ -160,6 +140,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
160140
f"then update {_package_label}.",
161141
FutureWarning)
162142

143+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
144+
"""Safely converts a semantic version string to a comparable tuple of integers.
145+
Example: "4.25.8" -> (4, 25, 8)
146+
Ignores non-numeric parts and handles common version formats.
147+
Args:
148+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
149+
Returns:
150+
Tuple of integers for the parsed version string.
151+
"""
152+
parts = []
153+
for part in version_string.split("."):
154+
try:
155+
parts.append(int(part))
156+
except ValueError:
157+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
158+
# This is a simplification compared to 'packaging.parse_version', but sufficient
159+
# for comparing strictly numeric semantic versions.
160+
break
161+
return tuple(parts)
162+
163163
def _get_version(dependency_name):
164164
try:
165165
version_string: str = metadata.version(dependency_name)

tests/integration/goldens/eventarc/google/cloud/eventarc_v1/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,6 @@
7272

7373
ParsedVersion = Tuple[int, ...]
7474

75-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
76-
"""Safely converts a semantic version string to a comparable tuple of integers.
77-
Example: "4.25.8" -> (4, 25, 8)
78-
Ignores non-numeric parts and handles common version formats.
79-
Args:
80-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
81-
Returns:
82-
Tuple of integers for the parsed version string.
83-
"""
84-
parts = []
85-
for part in version_string.split("."):
86-
try:
87-
parts.append(int(part))
88-
except ValueError:
89-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
90-
# This is a simplification compared to 'packaging.parse_version', but sufficient
91-
# for comparing strictly numeric semantic versions.
92-
break
93-
return tuple(parts)
94-
9575
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
9676
api_core.check_python_version("google.cloud.eventarc_v1") # type: ignore
9777
api_core.check_dependency_versions("google.cloud.eventarc_v1") # type: ignore
@@ -120,6 +100,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
120100
f"then update {_package_label}.",
121101
FutureWarning)
122102

103+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
104+
"""Safely converts a semantic version string to a comparable tuple of integers.
105+
Example: "4.25.8" -> (4, 25, 8)
106+
Ignores non-numeric parts and handles common version formats.
107+
Args:
108+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
109+
Returns:
110+
Tuple of integers for the parsed version string.
111+
"""
112+
parts = []
113+
for part in version_string.split("."):
114+
try:
115+
parts.append(int(part))
116+
except ValueError:
117+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
118+
# This is a simplification compared to 'packaging.parse_version', but sufficient
119+
# for comparing strictly numeric semantic versions.
120+
break
121+
return tuple(parts)
122+
123123
def _get_version(dependency_name):
124124
try:
125125
version_string: str = metadata.version(dependency_name)

tests/integration/goldens/logging/google/cloud/logging_v2/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@
115115

116116
ParsedVersion = Tuple[int, ...]
117117

118-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
119-
"""Safely converts a semantic version string to a comparable tuple of integers.
120-
Example: "4.25.8" -> (4, 25, 8)
121-
Ignores non-numeric parts and handles common version formats.
122-
Args:
123-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
124-
Returns:
125-
Tuple of integers for the parsed version string.
126-
"""
127-
parts = []
128-
for part in version_string.split("."):
129-
try:
130-
parts.append(int(part))
131-
except ValueError:
132-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
133-
# This is a simplification compared to 'packaging.parse_version', but sufficient
134-
# for comparing strictly numeric semantic versions.
135-
break
136-
return tuple(parts)
137-
138118
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
139119
api_core.check_python_version("google.cloud.logging_v2") # type: ignore
140120
api_core.check_dependency_versions("google.cloud.logging_v2") # type: ignore
@@ -163,6 +143,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
163143
f"then update {_package_label}.",
164144
FutureWarning)
165145

146+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
147+
"""Safely converts a semantic version string to a comparable tuple of integers.
148+
Example: "4.25.8" -> (4, 25, 8)
149+
Ignores non-numeric parts and handles common version formats.
150+
Args:
151+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
152+
Returns:
153+
Tuple of integers for the parsed version string.
154+
"""
155+
parts = []
156+
for part in version_string.split("."):
157+
try:
158+
parts.append(int(part))
159+
except ValueError:
160+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
161+
# This is a simplification compared to 'packaging.parse_version', but sufficient
162+
# for comparing strictly numeric semantic versions.
163+
break
164+
return tuple(parts)
165+
166166
def _get_version(dependency_name):
167167
try:
168168
version_string: str = metadata.version(dependency_name)

tests/integration/goldens/logging_internal/google/cloud/logging_v2/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,6 @@
115115

116116
ParsedVersion = Tuple[int, ...]
117117

118-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
119-
"""Safely converts a semantic version string to a comparable tuple of integers.
120-
Example: "4.25.8" -> (4, 25, 8)
121-
Ignores non-numeric parts and handles common version formats.
122-
Args:
123-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
124-
Returns:
125-
Tuple of integers for the parsed version string.
126-
"""
127-
parts = []
128-
for part in version_string.split("."):
129-
try:
130-
parts.append(int(part))
131-
except ValueError:
132-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
133-
# This is a simplification compared to 'packaging.parse_version', but sufficient
134-
# for comparing strictly numeric semantic versions.
135-
break
136-
return tuple(parts)
137-
138118
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
139119
api_core.check_python_version("google.cloud.logging_v2") # type: ignore
140120
api_core.check_dependency_versions("google.cloud.logging_v2") # type: ignore
@@ -163,6 +143,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
163143
f"then update {_package_label}.",
164144
FutureWarning)
165145

146+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
147+
"""Safely converts a semantic version string to a comparable tuple of integers.
148+
Example: "4.25.8" -> (4, 25, 8)
149+
Ignores non-numeric parts and handles common version formats.
150+
Args:
151+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
152+
Returns:
153+
Tuple of integers for the parsed version string.
154+
"""
155+
parts = []
156+
for part in version_string.split("."):
157+
try:
158+
parts.append(int(part))
159+
except ValueError:
160+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
161+
# This is a simplification compared to 'packaging.parse_version', but sufficient
162+
# for comparing strictly numeric semantic versions.
163+
break
164+
return tuple(parts)
165+
166166
def _get_version(dependency_name):
167167
try:
168168
version_string: str = metadata.version(dependency_name)

tests/integration/goldens/redis/google/cloud/redis_v1/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,6 @@
6262

6363
ParsedVersion = Tuple[int, ...]
6464

65-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
66-
"""Safely converts a semantic version string to a comparable tuple of integers.
67-
Example: "4.25.8" -> (4, 25, 8)
68-
Ignores non-numeric parts and handles common version formats.
69-
Args:
70-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
71-
Returns:
72-
Tuple of integers for the parsed version string.
73-
"""
74-
parts = []
75-
for part in version_string.split("."):
76-
try:
77-
parts.append(int(part))
78-
except ValueError:
79-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
80-
# This is a simplification compared to 'packaging.parse_version', but sufficient
81-
# for comparing strictly numeric semantic versions.
82-
break
83-
return tuple(parts)
84-
8565
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
8666
api_core.check_python_version("google.cloud.redis_v1") # type: ignore
8767
api_core.check_dependency_versions("google.cloud.redis_v1") # type: ignore
@@ -110,6 +90,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
11090
f"then update {_package_label}.",
11191
FutureWarning)
11292

93+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
94+
"""Safely converts a semantic version string to a comparable tuple of integers.
95+
Example: "4.25.8" -> (4, 25, 8)
96+
Ignores non-numeric parts and handles common version formats.
97+
Args:
98+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
99+
Returns:
100+
Tuple of integers for the parsed version string.
101+
"""
102+
parts = []
103+
for part in version_string.split("."):
104+
try:
105+
parts.append(int(part))
106+
except ValueError:
107+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
108+
# This is a simplification compared to 'packaging.parse_version', but sufficient
109+
# for comparing strictly numeric semantic versions.
110+
break
111+
return tuple(parts)
112+
113113
def _get_version(dependency_name):
114114
try:
115115
version_string: str = metadata.version(dependency_name)

tests/integration/goldens/redis_selective/google/cloud/redis_v1/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@
4949

5050
ParsedVersion = Tuple[int, ...]
5151

52-
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
53-
"""Safely converts a semantic version string to a comparable tuple of integers.
54-
Example: "4.25.8" -> (4, 25, 8)
55-
Ignores non-numeric parts and handles common version formats.
56-
Args:
57-
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
58-
Returns:
59-
Tuple of integers for the parsed version string.
60-
"""
61-
parts = []
62-
for part in version_string.split("."):
63-
try:
64-
parts.append(int(part))
65-
except ValueError:
66-
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
67-
# This is a simplification compared to 'packaging.parse_version', but sufficient
68-
# for comparing strictly numeric semantic versions.
69-
break
70-
return tuple(parts)
71-
7252
if hasattr(api_core, "check_python_version") and hasattr(api_core, "check_dependency_versions"): # pragma: NO COVER
7353
api_core.check_python_version("google.cloud.redis_v1") # type: ignore
7454
api_core.check_dependency_versions("google.cloud.redis_v1") # type: ignore
@@ -97,6 +77,26 @@ def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO
9777
f"then update {_package_label}.",
9878
FutureWarning)
9979

80+
def parse_version_to_tuple(version_string: str) -> ParsedVersion: # pragma: NO COVER
81+
"""Safely converts a semantic version string to a comparable tuple of integers.
82+
Example: "4.25.8" -> (4, 25, 8)
83+
Ignores non-numeric parts and handles common version formats.
84+
Args:
85+
version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
86+
Returns:
87+
Tuple of integers for the parsed version string.
88+
"""
89+
parts = []
90+
for part in version_string.split("."):
91+
try:
92+
parts.append(int(part))
93+
except ValueError:
94+
# If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
95+
# This is a simplification compared to 'packaging.parse_version', but sufficient
96+
# for comparing strictly numeric semantic versions.
97+
break
98+
return tuple(parts)
99+
100100
def _get_version(dependency_name):
101101
try:
102102
version_string: str = metadata.version(dependency_name)

0 commit comments

Comments
 (0)