Pylance-release: Pylance type checking rejects gRPC protobuf generated types

Created on 9 Sep 2020  路  2Comments  路  Source: microsoft/pylance-release

Environment data

  • Language Server version: Python 2020.8.106424 (2 September 2020); Pylance 2020.9.0 (3 September 2020)

  • OS and version: OSX 10.14.6 (18G84)
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.5 64-bit
  • Expected behaviour

    Pylance type checking should accept gRPC tools generated protobuf types in type hints

    Actual behaviour

    Pylance reports Expected class type but received "GeneratedProtocolMessageType"

    Code Snippet / Additional information


    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:

    Screenshot 2020-09-09 at 21 19 46

    waiting for info

    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:

    protoc -I. --python_out=. --grpc_python_out=. __mypy_out=. <filename.proto>
    

    All 2 comments

    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

    Was this page helpful?
    0 / 5 - 0 ratings

    Related issues

    flaree picture flaree  路  4Comments

    ciaranjudge picture ciaranjudge  路  3Comments

    fbidu picture fbidu  路  3Comments

    BrunoBlanes picture BrunoBlanes  路  4Comments

    peach-lasagna picture peach-lasagna  路  3Comments