Skip to content

Commit 9149df8

Browse files
committed
refactor(api-core): use PEP 604 union syntax in grpc_helpers.py and clarify reverse execution
1 parent 7aeb5a2 commit 9149df8

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Optional,
2525
Sequence,
2626
TypeVar,
27-
Union,
2827
get_args,
2928
)
3029

@@ -44,12 +43,12 @@
4443
P = TypeVar("P")
4544

4645
# Type alias representing any client-side gRPC interceptor
47-
ClientInterceptor = Union[
48-
grpc.UnaryUnaryClientInterceptor,
49-
grpc.UnaryStreamClientInterceptor,
50-
grpc.StreamUnaryClientInterceptor,
51-
grpc.StreamStreamClientInterceptor,
52-
]
46+
ClientInterceptor = (
47+
grpc.UnaryUnaryClientInterceptor
48+
| grpc.UnaryStreamClientInterceptor
49+
| grpc.StreamUnaryClientInterceptor
50+
| grpc.StreamStreamClientInterceptor
51+
)
5352

5453
# Runtime tuple of gRPC client interceptor base classes for isinstance checks
5554
_CLIENT_INTERCEPTOR_CLASSES = get_args(ClientInterceptor)
@@ -58,7 +57,7 @@
5857
ChannelWrapperCallable = Callable[[grpc.Channel], grpc.Channel]
5958

6059
# Generic type alias representing any channel wrapper (interceptor or callable)
61-
ChannelWrapper = Union[ClientInterceptor, ChannelWrapperCallable]
60+
ChannelWrapper = ClientInterceptor | ChannelWrapperCallable
6261

6362

6463
def _patch_callable_name(callable_):
@@ -447,7 +446,7 @@ def _modify_target_for_direct_path(target: str) -> str:
447446

448447
def apply_channel_wrappers(
449448
channel: grpc.Channel,
450-
wrappers: Optional[Sequence[ChannelWrapper]] = None,
449+
wrappers: Sequence[ChannelWrapper] | None = None,
451450
) -> grpc.Channel:
452451
"""Applies channel wrappers (client interceptors or channel-wrapping callables) to a gRPC channel.
453452
@@ -472,6 +471,7 @@ def apply_channel_wrappers(
472471
return channel
473472

474473
modified_channel = channel
474+
# Reverse the inputs to align with the behavior of grpc.create_channel(*interceptors)
475475
for wrapper in reversed(list(wrappers)):
476476
if isinstance(wrapper, _CLIENT_INTERCEPTOR_CLASSES):
477477
modified_channel = grpc.intercept_channel(modified_channel, wrapper)

0 commit comments

Comments
 (0)