|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +SCRIPT_PYTHON_315 = """ |
| 21 | +import sys |
| 22 | +import google.api_core.gapic_v1 |
| 23 | +
|
| 24 | +# The inner gapic_v1 modules should NOT be loaded yet |
| 25 | +HEAVY_MODULES = [ |
| 26 | + "google.api_core.gapic_v1.client_info", |
| 27 | + "google.api_core.gapic_v1.config", |
| 28 | + "google.api_core.gapic_v1.config_async", |
| 29 | + "google.api_core.gapic_v1.method", |
| 30 | + "google.api_core.gapic_v1.method_async", |
| 31 | + "google.api_core.gapic_v1.routing_header", |
| 32 | +] |
| 33 | +
|
| 34 | +for mod in HEAVY_MODULES: |
| 35 | + if mod in sys.modules: |
| 36 | + print(f"FAILED: {mod} was eagerly loaded into sys.modules") |
| 37 | + sys.exit(1) |
| 38 | +
|
| 39 | +# Trigger reification |
| 40 | +import google.api_core.gapic_v1.client_info |
| 41 | +_ = google.api_core.gapic_v1.client_info.__name__ |
| 42 | +
|
| 43 | +if "google.api_core.gapic_v1.client_info" not in sys.modules: |
| 44 | + print("FAILED: google.api_core.gapic_v1.client_info was not lazily loaded upon access") |
| 45 | + sys.exit(2) |
| 46 | +
|
| 47 | +sys.exit(0) |
| 48 | +""" |
| 49 | + |
| 50 | +SCRIPT_PRE_315 = """ |
| 51 | +import sys |
| 52 | +import google.api_core.gapic_v1 |
| 53 | +
|
| 54 | +HEAVY_MODULES = [ |
| 55 | + "google.api_core.gapic_v1.client_info", |
| 56 | + "google.api_core.gapic_v1.config", |
| 57 | + "google.api_core.gapic_v1.config_async", |
| 58 | + "google.api_core.gapic_v1.method", |
| 59 | + "google.api_core.gapic_v1.method_async", |
| 60 | + "google.api_core.gapic_v1.routing_header", |
| 61 | +] |
| 62 | +
|
| 63 | +for mod in HEAVY_MODULES: |
| 64 | + if mod not in sys.modules: |
| 65 | + print(f"FAILED: {mod} was not eagerly loaded into sys.modules") |
| 66 | + sys.exit(1) |
| 67 | +
|
| 68 | +sys.exit(0) |
| 69 | +""" |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.skipif(sys.version_info < (3, 15), reason="PEP 810 requires Python 3.15+") |
| 73 | +def test_lazy_imports_on_python_315(): |
| 74 | + result = subprocess.run( |
| 75 | + [sys.executable, "-c", SCRIPT_PYTHON_315], capture_output=True, text=True |
| 76 | + ) |
| 77 | + assert result.returncode == 0, f"Subprocess failed: {result.stdout}" |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skipif( |
| 81 | + sys.version_info >= (3, 15), reason="Testing fallback behavior on < 3.15" |
| 82 | +) |
| 83 | +def test_fallback_eager_imports_pre_315(): |
| 84 | + result = subprocess.run( |
| 85 | + [sys.executable, "-c", SCRIPT_PRE_315], capture_output=True, text=True |
| 86 | + ) |
| 87 | + assert result.returncode == 0, f"Subprocess failed: {result.stdout}" |
0 commit comments