gpt4 book ai didi

python - 你如何告诉 pylint protobuf 生成的对象的成员是什么?

转载 作者:太空狗 更新时间:2023-10-29 17:51:20 27 4
gpt4 key购买 nike

我想为一组 protobuf 消息发布一个 python 包。 protobuf 编译器 (protoc) 生成一个 python 库,它实际上并不定义典型意义上的类型/类,而是动态构造它们。有什么方法可以提示 pylint 这些类的成员和字段是什么?

例如,考虑以下简单的 protobuf 消息规范:

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}

编译器生成了如下一长串代码:

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: test.proto

import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor.FileDescriptor(
name='test.proto',
package='',
serialized_pb=_b('\n\ntest.proto\"1\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)




_PERSON = _descriptor.Descriptor(
name='Person',
full_name='Person',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='Person.name', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='id', full_name='Person.id', index=1,
number=2, type=5, cpp_type=1, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
_descriptor.FieldDescriptor(
name='email', full_name='Person.email', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
options=None),
],
extensions=[
],
nested_types=[],
enum_types=[
],
options=None,
is_extendable=False,
extension_ranges=[],
oneofs=[
],
serialized_start=14,
serialized_end=63,
)

DESCRIPTOR.message_types_by_name['Person'] = _PERSON

Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(
DESCRIPTOR = _PERSON,
__module__ = 'test_pb2'
# @@protoc_insertion_point(class_scope:Person)
))
_sym_db.RegisterMessage(Person)


# @@protoc_insertion_point(module_scope)

最佳答案

似乎不支持自动向 Pylint 提示 protobuf 消息中字段的名称,因此我拼凑了一个 Pylint 的小扩展来静态确定这些。

介绍 pylint-protobuf :

from example_pb2 import Person
p = Person()
p.invalid_field = 123

用法:

$ pip install pylint-protobuf
$ pylint --load-plugins=pylint_protobuf example.py
************* Module example
E: 3, 0: Field 'invalid_field' does not appear in the declared
fields of protobuf-generated class 'Person' and will raise
AttributeError on access (protobuf-undefined-attribute)

公平警告:它还处于 alpha 阶段,不支持的功能(目前!)包括嵌套导入和重复字段。

关于python - 你如何告诉 pylint protobuf 生成的对象的成员是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com