Skip to content

Commit 8e5e0ad

Browse files
committed
fix(secretmanager): resolve ClientInterceptor type definition and test mocks for older google-api-core
1 parent 0e1c21b commit 8e5e0ad

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

packages/google-cloud-secret-manager/google/cloud/secretmanager_v1/services/secret_manager_service/transports/grpc.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import logging as std_logging
1818
import pickle
1919
import warnings
20-
from typing import Callable, Dict, Optional, Sequence, Tuple, Union
20+
from typing import Any, Callable, Dict, Optional, Sequence, Tuple, Union
2121

2222
import google.auth # type: ignore
2323
import google.iam.v1.iam_policy_pb2 as iam_policy_pb2 # type: ignore
@@ -27,21 +27,22 @@
2727
import grpc # type: ignore
2828
import proto # type: ignore
2929
from google.api_core import gapic_v1, grpc_helpers
30-
31-
try:
32-
# mypy: ClientInterceptor was added in google-api-core 2.35.0; guard for older versions
33-
from google.api_core.grpc_helpers import ClientInterceptor # type: ignore[attr-defined]
34-
except ImportError:
35-
ClientInterceptor = grpc.ClientInterceptor # type: ignore[misc,assignment]
3630
from google.auth import credentials as ga_credentials # type: ignore
3731
from google.auth.transport.grpc import SslCredentials # type: ignore
3832
from google.cloud.location import locations_pb2 # type: ignore
39-
from google.protobuf.json_format import MessageToJson
40-
4133
from google.cloud.secretmanager_v1.types import resources, service
34+
from google.protobuf.json_format import MessageToJson
4235

4336
from .base import DEFAULT_CLIENT_INFO, SecretManagerServiceTransport
4437

38+
# ClientInterceptor type alias for channel interceptors
39+
ClientInterceptor = Union[
40+
grpc.UnaryUnaryClientInterceptor,
41+
grpc.UnaryStreamClientInterceptor,
42+
grpc.StreamUnaryClientInterceptor,
43+
grpc.StreamStreamClientInterceptor,
44+
]
45+
4546
try:
4647
from google.api_core import client_logging # type: ignore
4748

packages/google-cloud-secret-manager/tests/unit/gapic/secretmanager_v1/test_secret_manager_service.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@
6262
from google.auth import credentials as ga_credentials
6363
from google.auth.exceptions import MutualTLSChannelError
6464
from google.cloud.location import locations_pb2
65-
from google.oauth2 import service_account
66-
6765
from google.cloud.secretmanager_v1.services.secret_manager_service import (
6866
SecretManagerServiceAsyncClient,
6967
SecretManagerServiceClient,
7068
pagers,
7169
transports,
7270
)
7371
from google.cloud.secretmanager_v1.types import resources, service
72+
from google.oauth2 import service_account
7473

7574
CRED_INFO_JSON = {
7675
"credential_source": "/path/to/file",
@@ -486,18 +485,20 @@ def test_secret_manager_service_client_otel_channel_injection_enabled():
486485
allowing the Transport to apply it via apply_channel_interceptors.
487486
"""
488487
mock_interceptor = mock.Mock()
488+
mock_obs = mock.Mock()
489+
mock_obs.get_otel_interceptor.return_value = mock_interceptor
489490
with (
490491
mock.patch(
491-
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability.get_otel_interceptor",
492-
return_value=mock_interceptor,
493-
) as mock_get_interceptor,
492+
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability",
493+
mock_obs,
494+
),
494495
mock.patch.object(
495496
transports.SecretManagerServiceGrpcTransport, "__init__", return_value=None
496497
) as patched_transport_init,
497498
):
498499
client = SecretManagerServiceClient(transport="grpc")
499500

500-
mock_get_interceptor.assert_called_once_with(client._client_options)
501+
mock_obs.get_otel_interceptor.assert_called_once_with(client._client_options)
501502
called_kwargs = patched_transport_init.call_args.kwargs
502503
assert "interceptors" in called_kwargs
503504
assert called_kwargs["interceptors"] == [mock_interceptor]
@@ -509,18 +510,20 @@ def test_secret_manager_service_client_otel_channel_injection_disabled():
509510
1. SecretManagerServiceClient checks for an OTel interceptor and receives None.
510511
2. No OTel interceptor is added to the transport constructor kwargs.
511512
"""
513+
mock_obs = mock.Mock()
514+
mock_obs.get_otel_interceptor.return_value = None
512515
with (
513516
mock.patch(
514-
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability.get_otel_interceptor",
515-
return_value=None,
516-
) as mock_get_interceptor,
517+
"google.cloud.secretmanager_v1.services.secret_manager_service.client._observability",
518+
mock_obs,
519+
),
517520
mock.patch.object(
518521
transports.SecretManagerServiceGrpcTransport, "__init__", return_value=None
519522
) as patched_transport_init,
520523
):
521524
client = SecretManagerServiceClient(transport="grpc")
522525

523-
mock_get_interceptor.assert_called_once_with(client._client_options)
526+
mock_obs.get_otel_interceptor.assert_called_once_with(client._client_options)
524527
called_kwargs = patched_transport_init.call_args.kwargs
525528
interceptors = called_kwargs.get("interceptors", [])
526529
assert not interceptors
@@ -561,6 +564,7 @@ def test_secret_manager_service_grpc_transport_interceptors():
561564
mock.patch(
562565
"google.api_core.grpc_helpers.apply_channel_interceptors",
563566
return_value=mock_channel,
567+
create=True,
564568
) as mock_apply_interceptors,
565569
):
566570
transports.SecretManagerServiceGrpcTransport(
@@ -582,6 +586,7 @@ def test_secret_manager_service_grpc_transport_custom_channel_interceptors():
582586
with mock.patch(
583587
"google.api_core.grpc_helpers.apply_channel_interceptors",
584588
return_value=mock_custom_channel,
589+
create=True,
585590
) as mock_apply_interceptors:
586591
transports.SecretManagerServiceGrpcTransport(
587592
channel=mock_custom_channel,

0 commit comments

Comments
 (0)