Skip to content

Commit f89cfbd

Browse files
committed
fix(api-core): add TypeAlias and typing.cast to satisfy mypy
1 parent 9149df8 commit f89cfbd

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

packages/google-api-core/google/api_core/grpc_helpers.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
Iterator,
2424
Optional,
2525
Sequence,
26+
TypeAlias,
2627
TypeVar,
28+
cast,
2729
get_args,
2830
)
2931

@@ -43,7 +45,7 @@
4345
P = TypeVar("P")
4446

4547
# Type alias representing any client-side gRPC interceptor
46-
ClientInterceptor = (
48+
ClientInterceptor: TypeAlias = (
4749
grpc.UnaryUnaryClientInterceptor
4850
| grpc.UnaryStreamClientInterceptor
4951
| grpc.StreamUnaryClientInterceptor
@@ -54,10 +56,10 @@
5456
_CLIENT_INTERCEPTOR_CLASSES = get_args(ClientInterceptor)
5557

5658
# Type alias representing a channel-wrapping callable
57-
ChannelWrapperCallable = Callable[[grpc.Channel], grpc.Channel]
59+
ChannelWrapperCallable: TypeAlias = Callable[[grpc.Channel], grpc.Channel]
5860

5961
# Generic type alias representing any channel wrapper (interceptor or callable)
60-
ChannelWrapper = ClientInterceptor | ChannelWrapperCallable
62+
ChannelWrapper: TypeAlias = ClientInterceptor | ChannelWrapperCallable
6163

6264

6365
def _patch_callable_name(callable_):
@@ -476,7 +478,8 @@ def apply_channel_wrappers(
476478
if isinstance(wrapper, _CLIENT_INTERCEPTOR_CLASSES):
477479
modified_channel = grpc.intercept_channel(modified_channel, wrapper)
478480
elif callable(wrapper):
479-
modified_channel = wrapper(modified_channel)
481+
wrapper_callable = cast(ChannelWrapperCallable, wrapper)
482+
modified_channel = wrapper_callable(modified_channel)
480483
else:
481484
raise TypeError(
482485
f"Expected ChannelWrapper (ClientInterceptor or Callable[[Channel], Channel]), got {type(wrapper).__name__}"

0 commit comments

Comments
 (0)