Skip to content

Commit 204b6d0

Browse files
authored
test(gapic-generator): Update gapic generator to resolve pytest-asyncio errors. (#17260)
This PR updates the Python GAPIC generator templates to support modern EOL Python support drops natively across the entire SDK. ### Changes * **setup.py.j2**: Bumps the baseline minimum `grpcio` dependency requirement to `>= 1.59.0`. This robust minimum requirement resolves the `pytest-asyncio` event loop regressions universally, removing the need for custom `pytest-asyncio` overrides. * **noxfile.py.j2**: Replaced the hardcoded constraints path with a dynamic constraints file path resolver at runtime, ensuring that monorepos natively resolve to their version-specific constraints files (e.g., `testing/constraints-3.10.txt`). * **pytest-asyncio** Updates the gapic-generator in order to resolve an issue with `pytest-asyncio` failures across Python runtimes 3.10 - 3.14.
1 parent 4f8a6c8 commit 204b6d0

45 files changed

Lines changed: 287 additions & 31 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/gapic-generator/gapic/templates/noxfile.py.j2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ PREVIEW_PYTHON_VERSION = "3.14"
4141

4242
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
4343

44-
LOWER_BOUND_CONSTRAINTS_FILE = CURRENT_DIRECTORY / "constraints.txt"
44+
if (CURRENT_DIRECTORY / "testing").exists():
45+
LOWER_BOUND_CONSTRAINTS_FILE = (
46+
CURRENT_DIRECTORY / "testing" / f"constraints-{ALL_PYTHON[0]}.txt"
47+
)
48+
else:
49+
LOWER_BOUND_CONSTRAINTS_FILE = CURRENT_DIRECTORY / "constraints.txt"
4550
PACKAGE_NAME = "{{ api.naming.warehouse_package_name }}"
4651

4752
UNIT_TEST_STANDARD_DEPENDENCIES = [

packages/gapic-generator/gapic/templates/setup.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies = [
3737
# Exclude incompatible versions of `google-auth`
3838
# See https://github.com/googleapis/google-cloud-python/issues/12364
3939
"google-auth >= 2.14.1, <3.0.0,!=2.24.0,!=2.25.0",
40-
"grpcio >= 1.44.0, < 2.0.0",
40+
"grpcio >= 1.59.0, < 2.0.0",
4141
"grpcio >= 1.75.1, < 2.0.0; python_version >= '3.14'",
4242
"proto-plus >= 1.22.3, <2.0.0",
4343
"proto-plus >= 1.25.0, <2.0.0; python_version >= '3.13'",

packages/gapic-generator/gapic/templates/testing/constraints-3.10-async-rest.txt.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# then this file should have google-cloud-foo==1.14.0
1111
google-api-core==2.21.0
1212
google-auth==2.35.0
13-
grpcio==1.44.0
13+
grpcio==1.59.0
1414
proto-plus==1.22.3
1515
protobuf==4.25.8
1616
{% for package_tuple, package_info in pypi_packages.items() %}

packages/gapic-generator/gapic/templates/testing/constraints-3.10.txt.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# then this file should have google-cloud-foo==1.14.0
88
google-api-core==2.17.1
99
google-auth==2.14.1
10-
grpcio==1.44.0
10+
grpcio==1.59.0
1111
proto-plus==1.22.3
1212
protobuf==4.25.8
1313
{% for package_tuple, package_info in pypi_packages.items() %}

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
88

99
import os
10+
import asyncio
1011
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|select|list %}
1112
import re
1213
{% endif %}
@@ -140,6 +141,21 @@ def modify_default_endpoint_template(client):
140141
return "test.{UNIVERSE_DOMAIN}" if ("localhost" in client._DEFAULT_ENDPOINT_TEMPLATE) else client._DEFAULT_ENDPOINT_TEMPLATE
141142

142143

144+
@pytest.fixture(autouse=True)
145+
def set_event_loop():
146+
try:
147+
asyncio.get_running_loop()
148+
yield
149+
except RuntimeError:
150+
loop = asyncio.new_event_loop()
151+
asyncio.set_event_loop(loop)
152+
try:
153+
yield
154+
finally:
155+
loop.close()
156+
asyncio.set_event_loop(None)
157+
158+
143159
def test__get_default_mtls_endpoint():
144160
api_endpoint = "example.googleapis.com"
145161
api_mtls_endpoint = "example.mtls.googleapis.com"

packages/gapic-generator/noxfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ def run_showcase_unit_tests(session, fail_under=100, rest_async_io_enabled=False
482482
"pytest-xdist",
483483
"pytest-asyncio",
484484
)
485+
# Freeze and print python environment package versions
486+
session.run("python", "-m", "pip", "freeze")
487+
485488
# Run the tests.
486489
session.run(
487490
"py.test",

packages/gapic-generator/rules_python_gapic/test/integration_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _overwrite_golden_impl(ctx):
114114
golden_update_script_content = """
115115
cd ${{BUILD_WORKSPACE_DIRECTORY}}
116116
# Filename pattern-based removal is needed to preserve the BUILD.bazel file.
117-
find tests/integration/goldens/{api_name}/ -name \\*.py-type f -delete
117+
find tests/integration/goldens/{api_name}/ -name \\*.py -type f -delete
118118
find tests/integration/goldens/{api_name}/ -name \\*.json -type f -delete
119119
unzip -ao {goldens_output_zip} -d tests/integration/goldens/{api_name}
120120
""".format(

packages/gapic-generator/tests/integration/goldens/asset/noxfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@
4848

4949
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
5050

51-
LOWER_BOUND_CONSTRAINTS_FILE = CURRENT_DIRECTORY / "constraints.txt"
51+
if (CURRENT_DIRECTORY / "testing").exists():
52+
LOWER_BOUND_CONSTRAINTS_FILE = (
53+
CURRENT_DIRECTORY / "testing" / f"constraints-{ALL_PYTHON[0]}.txt"
54+
)
55+
else:
56+
LOWER_BOUND_CONSTRAINTS_FILE = CURRENT_DIRECTORY / "constraints.txt"
5257
PACKAGE_NAME = "google-cloud-asset"
5358

5459
UNIT_TEST_STANDARD_DEPENDENCIES = [

packages/gapic-generator/tests/integration/goldens/asset/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# Exclude incompatible versions of `google-auth`
4444
# See https://github.com/googleapis/google-cloud-python/issues/12364
4545
"google-auth >= 2.14.1, <3.0.0,!=2.24.0,!=2.25.0",
46-
"grpcio >= 1.44.0, < 2.0.0",
46+
"grpcio >= 1.59.0, < 2.0.0",
4747
"grpcio >= 1.75.1, < 2.0.0; python_version >= '3.14'",
4848
"proto-plus >= 1.22.3, <2.0.0",
4949
"proto-plus >= 1.25.0, <2.0.0; python_version >= '3.13'",

packages/gapic-generator/tests/integration/goldens/asset/testing/constraints-3.10.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# then this file should have google-cloud-foo==1.14.0
77
google-api-core==2.17.1
88
google-auth==2.14.1
9-
grpcio==1.44.0
9+
grpcio==1.59.0
1010
proto-plus==1.22.3
1111
protobuf==4.25.8
1212
google-cloud-access-context-manager==0.2.0

0 commit comments

Comments
 (0)