|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import importlib.util |
15 | 16 | from typing import Set |
16 | 17 |
|
| 18 | +_has_grpc = importlib.util.find_spec("grpc") is not None |
| 19 | + |
17 | 20 | # PEP 0810: Explicit Lazy Imports |
18 | 21 | # Python 3.15+ natively intercepts and defers these imports. |
19 | 22 | # Developers can disable this behavior and force eager imports. |
|
22 | 25 | # Older Python versions safely ignore this variable. |
23 | 26 | __lazy_modules__: Set[str] = { |
24 | 27 | "google.api_core.gapic_v1.client_info", |
25 | | - "google.api_core.gapic_v1.config", |
26 | | - "google.api_core.gapic_v1.config_async", |
27 | | - "google.api_core.gapic_v1.method", |
28 | | - "google.api_core.gapic_v1.method_async", |
29 | 28 | "google.api_core.gapic_v1.routing_header", |
30 | 29 | } |
31 | 30 |
|
32 | | -from google.api_core.gapic_v1 import client_info |
33 | | -from google.api_core.gapic_v1 import routing_header |
| 31 | +if _has_grpc: |
| 32 | + __lazy_modules__.update( |
| 33 | + { |
| 34 | + "google.api_core.gapic_v1.config", |
| 35 | + "google.api_core.gapic_v1.config_async", |
| 36 | + "google.api_core.gapic_v1.method", |
| 37 | + "google.api_core.gapic_v1.method_async", |
| 38 | + } |
| 39 | + ) |
34 | 40 |
|
35 | | -__all__ = [ |
36 | | - "client_info", |
37 | | - "routing_header", |
38 | | -] |
| 41 | +from google.api_core.gapic_v1 import client_info # noqa: E402 |
| 42 | +from google.api_core.gapic_v1 import routing_header # noqa: E402 |
39 | 43 |
|
40 | | -try: |
41 | | - from google.api_core.gapic_v1 import config |
42 | | - from google.api_core.gapic_v1 import config_async |
43 | | - from google.api_core.gapic_v1 import method |
44 | | - from google.api_core.gapic_v1 import method_async |
| 44 | +if _has_grpc: |
| 45 | + from google.api_core.gapic_v1 import config # noqa: F401 |
| 46 | + from google.api_core.gapic_v1 import config_async # noqa: F401 |
| 47 | + from google.api_core.gapic_v1 import method # noqa: F401 |
| 48 | + from google.api_core.gapic_v1 import method_async # noqa: F401 |
45 | 49 |
|
46 | | - __all__.extend( |
47 | | - [ |
48 | | - "config", |
49 | | - "config_async", |
50 | | - "method", |
51 | | - "method_async", |
52 | | - ] |
53 | | - ) |
54 | | -except ImportError: |
55 | | - pass |
| 50 | + __all__ = [ |
| 51 | + "client_info", |
| 52 | + "routing_header", |
| 53 | + "config", |
| 54 | + "config_async", |
| 55 | + "method", |
| 56 | + "method_async", |
| 57 | + ] |
| 58 | +else: |
| 59 | + __all__ = [ |
| 60 | + "client_info", |
| 61 | + "routing_header", |
| 62 | + ] |
0 commit comments