Skip to content

Allow None return from grpc.aio.ServerInterceptor.intercept_service#15783

Open
Rhadamanthus1 wants to merge 2 commits into
python:mainfrom
Rhadamanthus1:grpcio-server-interceptor-none-return
Open

Allow None return from grpc.aio.ServerInterceptor.intercept_service#15783
Rhadamanthus1 wants to merge 2 commits into
python:mainfrom
Rhadamanthus1:grpcio-server-interceptor-none-return

Conversation

@Rhadamanthus1
Copy link
Copy Markdown

Summary

Update the type hints for grpc.aio.ServerInterceptor.intercept_service and its continuation parameter to allow returning None. Specifically, the return type and the inner Awaitable are widened to RpcMethodHandler[_TRequest, _TResponse] | None.

Motivation

The runtime docstring for grpc.aio.ServerInterceptor.intercept_service explicitly documents None as a valid return value to signal that an RPC is not being serviced:

Returns: An RpcMethodHandler with which the RPC may be serviced if the interceptor chooses to service this RPC, or None otherwise.

Because the interceptor chain propagates None end-to-end (each link's continuation returns whatever the next interceptor returned), the inner Awaitable must also admit None. With the current stubs, subclasses that honour the documented contract are forced to use # type: ignore[override] to widen the override. This PR eliminates that friction.

Precedent

This change aligns the async stubs with their synchronous counterpart. The sync stub (stubs/grpcio/grpc/__init__.pyi, lines 506-510, see here) already correctly declares both positions as RpcMethodHandler[_TRequest, _TResponse] | None:

class ServerInterceptor(abc.ABC):
    @abc.abstractmethod
    def intercept_service(
        self,
        continuation: Callable[[HandlerCallDetails], RpcMethodHandler[_TRequest, _TResponse] | None],
        handler_call_details: HandlerCallDetails,
    ) -> RpcMethodHandler[_TRequest, _TResponse] | None: ...

Changes

  • stubs/grpcio/grpc/aio/__init__.pyi:
    • Widened the Awaitable[...] and the return type of ServerInterceptor.intercept_service to include None.
    • Added a short inline comment explaining the optional return type.

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant