Skip to content

Servicer stubs annotated are with @abstractmethod even though grpc implements them #680

@tenpura-shrimp

Description

@tenpura-shrimp

In the generated pyi code we have someting like this generated for the servicer type in the pyi.

class MyApiServicer(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def MyRpc(
        self,
        request: annotate_service_pb2.MyRpcRequest,
        context: _ServicerContext,
    ) ->...

But actually in the real py file we have an implementation:

class MyApiServicer(object):
    def MyRpc(self, request, context):
        context.set_code(grpc.StatusCode.UNIMPLEMENTED)
        context.set_details('Method not implemented!')
        raise NotImplementedError('Method not implemented!')

So in this case if we called this method, we would have no runtime error, and the grpc service will return the unimplemented status code as expected.

In our production use case, this is desired behavior, as we publish proto changes separately from making the code implementation. The expected behavior would allow us to build with new protos without making any python changes.

But with this annotation, it will fail the mypy checks because of the @abc.abstractmethod, unless the subclass explicitly overrides that method.

Solution:

@abc.abstractmethod should be removed from the generated servicer types, in order to match the fact that the grpc generated base servicer has a concrete implementation provided.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions