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