Skip to content

Commit 36b401d

Browse files
committed
fix: address code review comments for gapic_v1 lazy imports
1 parent e078bb4 commit 36b401d

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

  • packages/google-api-core/google/api_core/gapic_v1

packages/google-api-core/google/api_core/gapic_v1/__init__.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import importlib.util
1516
from typing import Set
1617

18+
_has_grpc = importlib.util.find_spec("grpc") is not None
19+
1720
# PEP 0810: Explicit Lazy Imports
1821
# Python 3.15+ natively intercepts and defers these imports.
1922
# Developers can disable this behavior and force eager imports.
@@ -22,34 +25,38 @@
2225
# Older Python versions safely ignore this variable.
2326
__lazy_modules__: Set[str] = {
2427
"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",
2928
"google.api_core.gapic_v1.routing_header",
3029
}
3130

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+
)
3440

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
3943

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
4549

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

Comments
 (0)