Language Server version: Python 2020.8.106424 (2 September 2020); Pylance 2020.9.0 (3 September 2020)
Pylance type checking should accept gRPC tools generated protobuf types in type hints
Pylance reports Expected class type but received "GeneratedProtocolMessageType"
Here is the gRPC tools generated type definition in a _pb2.py file:
Content = _reflection.GeneratedProtocolMessageType('Content', (_message.Message,), {
'DESCRIPTOR' : _CONTENT,
'__module__' : 'proto.tagging_service_pb2'
# @@protoc_insertion_point(class_scope:com.example.map.Content)
})
_sym_db.RegisterMessage(Content)
MLTags = _reflection.GeneratedProtocolMessageType('MLTags', (_message.Message,), {
'DESCRIPTOR' : _MLTAGS,
'__module__' : 'proto.tagging_service_pb2'
# @@protoc_insertion_point(class_scope:com.example.map.MLTags)
})
_sym_db.RegisterMessage(MLTags)
Here is a reference in a method signature:
from proto.tagging_service_pb2 import MLTags, Content
def EnrichContentWithTags(self, request: Content, context: ServicerContext) -> MLTags:
Here is the Pylance type-checking message:

The protoc tool generates Python that is not friendly to type checkers. For that reason, there's a protoc plugin that generates high-quality type stubs for proto files. It's called mypy-protobuf, and you can install it via pip.
Despite the fact that "mypy-protobuf" contains "mypy" in its name, it's not specific to mypy. It outputs standard type definition files that are understandable by all standards-compliant type checkers including Pyright, which is the type checker at the heart of Pylance.
When you generate the "_pb2.py" file, you should also generate the corresponding "_pb2.pyi" file as follows:
protoc -I. --python_out=. --grpc_python_out=. __mypy_out=. <filename.proto>
thanks... this fixed my issue
Most helpful comment
The protoc tool generates Python that is not friendly to type checkers. For that reason, there's a protoc plugin that generates high-quality type stubs for proto files. It's called mypy-protobuf, and you can install it via pip.
Despite the fact that "mypy-protobuf" contains "mypy" in its name, it's not specific to mypy. It outputs standard type definition files that are understandable by all standards-compliant type checkers including Pyright, which is the type checker at the heart of Pylance.
When you generate the "_pb2.py" file, you should also generate the corresponding "_pb2.pyi" file as follows: