I have created a simple Protobuf object (let's call it file_nam.proto
) like the one below:
syntax = "proto3";
package something.somethingelse;
message SomeName {
string first = 1;
string second = 2;
string third = 3;
string forth = 4;
uint64 fifth = 5;
uint64 sixth = 6;
}
which I then successfully compile into .py file using protobuf package. The issue is that when I try to import the .py
module in another module, mypy
returns the following error when I run the pre-commit
command.
interfaces/proto/file_name_pb2.py:28: error: Module has no attribute "_USE_C_DESCRIPTORS"
the error refers to the last line in
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: rasv2_task.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
.
.
.
if _descriptor._USE_C_DESCRIPTORS == False:
.
.
.
but it doesn't make any sense because _descriptor
does, in fact, have a _USE_C_DESCRIPTORS attribute.
CodePudding user response:
mypy does not understand the generated code from protobuf -- it does some meta-programmery things that confuse the type checker
but fortunately there's a project to generate .pyi
stubs that mypy can understand -- https://github.com/nipunn1313/mypy-protobuf
mypy will prefer the .pyi
files to the real .py
files and you'll be able to use those types elsewhere