|
| 1 | +import sys |
| 2 | +import pytest |
| 3 | + |
| 4 | +# List of modules we expect to be lazy |
| 5 | +LAZY_MODULES = [ |
| 6 | + "google.auth.transport.requests", |
| 7 | + "google.auth.transport.urllib3", |
| 8 | + "google.auth.transport.grpc", |
| 9 | +] |
| 10 | + |
| 11 | +def clean_sys_modules(): |
| 12 | + """Helper to ensure we start with a clean slate for import testing.""" |
| 13 | + for mod in LAZY_MODULES: |
| 14 | + sys.modules.pop(mod, None) |
| 15 | + |
| 16 | +@pytest.mark.skipif(sys.version_info < (3, 15), reason="PEP 810 requires Python 3.15+") |
| 17 | +def test_lazy_imports_on_python_315(): |
| 18 | + clean_sys_modules() |
| 19 | + |
| 20 | + # 1. Import the transport package |
| 21 | + import google.auth.transport |
| 22 | + |
| 23 | + # 2. Assert that none of the lazy modules have been eagerly loaded into sys.modules |
| 24 | + for mod in LAZY_MODULES: |
| 25 | + assert mod not in sys.modules |
| 26 | + |
| 27 | + # 3. Access an attribute to trigger reification |
| 28 | + from google.auth.transport import requests |
| 29 | + _ = requests.__name__ # Trigger first-use reification |
| 30 | + |
| 31 | + # 4. Assert that the module has now been reified and loaded |
| 32 | + assert "google.auth.transport.requests" in sys.modules |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.skipif(sys.version_info >= (3, 15), reason="Testing fallback behavior on < 3.15") |
| 36 | +def test_fallback_eager_imports_pre_315(): |
| 37 | + clean_sys_modules() |
| 38 | + |
| 39 | + # On older Python, __lazy_modules__ is safely ignored, meaning they should eager-load |
| 40 | + import google.auth.transport |
| 41 | + |
| 42 | + for mod in LAZY_MODULES: |
| 43 | + assert mod in sys.modules |
0 commit comments