From 0f81ca42a6cb90ddbe7e18a110e7f55f6f4a365c Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 15 Aug 2019 16:01:44 -0700 Subject: [PATCH] Add `mypy` protobufs plugin and regenerate protobufs --- Makefile | 4 +- libp2p/crypto/pb/crypto_pb2.pyi | 84 +++++++++++++++++++++++++++++++ libp2p/pubsub/pb/rpc_pb2.py | 88 ++++++++++++++++----------------- setup.py | 1 + 4 files changed, 131 insertions(+), 46 deletions(-) create mode 100644 libp2p/crypto/pb/crypto_pb2.pyi diff --git a/Makefile b/Makefile index 28a4582..d44fd6b 100644 --- a/Makefile +++ b/Makefile @@ -11,5 +11,5 @@ lintroll: flake8 $(FILES_TO_LINT) protobufs: - cd libp2p/crypto/pb && protoc --python_out=. crypto.proto - cd libp2p/pubsub/pb && protoc --python_out=. rpc.proto + cd libp2p/crypto/pb && protoc --python_out=. --mypy_out=. crypto.proto + cd libp2p/pubsub/pb && protoc --python_out=. --mypy_out=. rpc.proto diff --git a/libp2p/crypto/pb/crypto_pb2.pyi b/libp2p/crypto/pb/crypto_pb2.pyi new file mode 100644 index 0000000..2b3cb1b --- /dev/null +++ b/libp2p/crypto/pb/crypto_pb2.pyi @@ -0,0 +1,84 @@ +# @generated by generate_proto_mypy_stubs.py. Do not edit! +import sys +from google.protobuf.descriptor import ( + Descriptor as google___protobuf___descriptor___Descriptor, + EnumDescriptor as google___protobuf___descriptor___EnumDescriptor, +) + +from google.protobuf.message import ( + Message as google___protobuf___message___Message, +) + +from typing import ( + List as typing___List, + Tuple as typing___Tuple, + cast as typing___cast, +) + +from typing_extensions import ( + Literal as typing_extensions___Literal, +) + + +class KeyType(int): + DESCRIPTOR: google___protobuf___descriptor___EnumDescriptor = ... + @classmethod + def Name(cls, number: int) -> str: ... + @classmethod + def Value(cls, name: str) -> KeyType: ... + @classmethod + def keys(cls) -> typing___List[str]: ... + @classmethod + def values(cls) -> typing___List[KeyType]: ... + @classmethod + def items(cls) -> typing___List[typing___Tuple[str, KeyType]]: ... + RSA = typing___cast(KeyType, 0) + Ed25519 = typing___cast(KeyType, 1) + Secp256k1 = typing___cast(KeyType, 2) + ECDSA = typing___cast(KeyType, 3) +RSA = typing___cast(KeyType, 0) +Ed25519 = typing___cast(KeyType, 1) +Secp256k1 = typing___cast(KeyType, 2) +ECDSA = typing___cast(KeyType, 3) + +class PublicKey(google___protobuf___message___Message): + DESCRIPTOR: google___protobuf___descriptor___Descriptor = ... + key_type = ... # type: KeyType + data = ... # type: bytes + + def __init__(self, + *, + key_type : KeyType, + data : bytes, + ) -> None: ... + @classmethod + def FromString(cls, s: bytes) -> PublicKey: ... + def MergeFrom(self, other_msg: google___protobuf___message___Message) -> None: ... + def CopyFrom(self, other_msg: google___protobuf___message___Message) -> None: ... + if sys.version_info >= (3,): + def HasField(self, field_name: typing_extensions___Literal[u"data",u"key_type"]) -> bool: ... + def ClearField(self, field_name: typing_extensions___Literal[u"data",u"key_type"]) -> None: ... + else: + def HasField(self, field_name: typing_extensions___Literal[u"data",b"data",u"key_type",b"key_type"]) -> bool: ... + def ClearField(self, field_name: typing_extensions___Literal[u"data",b"data",u"key_type",b"key_type"]) -> None: ... + +class PrivateKey(google___protobuf___message___Message): + DESCRIPTOR: google___protobuf___descriptor___Descriptor = ... + key_type = ... # type: KeyType + data = ... # type: bytes + + def __init__(self, + *, + key_type : KeyType, + data : bytes, + ) -> None: ... + @classmethod + def FromString(cls, s: bytes) -> PrivateKey: ... + def MergeFrom(self, other_msg: google___protobuf___message___Message) -> None: ... + def CopyFrom(self, other_msg: google___protobuf___message___Message) -> None: ... + if sys.version_info >= (3,): + def HasField(self, field_name: typing_extensions___Literal[u"data",u"key_type"]) -> bool: ... + def ClearField(self, field_name: typing_extensions___Literal[u"data",u"key_type"]) -> None: ... + else: + def HasField(self, field_name: typing_extensions___Literal[u"data",b"data",u"key_type",b"key_type"]) -> bool: ... + def ClearField(self, field_name: typing_extensions___Literal[u"data",b"data",u"key_type",b"key_type"]) -> None: ... diff --git a/libp2p/pubsub/pb/rpc_pb2.py b/libp2p/pubsub/pb/rpc_pb2.py index e3a54a8..ab794f2 100644 --- a/libp2p/pubsub/pb/rpc_pb2.py +++ b/libp2p/pubsub/pb/rpc_pb2.py @@ -555,82 +555,82 @@ DESCRIPTOR.message_types_by_name['ControlPrune'] = _CONTROLPRUNE DESCRIPTOR.message_types_by_name['TopicDescriptor'] = _TOPICDESCRIPTOR _sym_db.RegisterFileDescriptor(DESCRIPTOR) -RPC = _reflection.GeneratedProtocolMessageType('RPC', (_message.Message,), { +RPC = _reflection.GeneratedProtocolMessageType('RPC', (_message.Message,), dict( - 'SubOpts' : _reflection.GeneratedProtocolMessageType('SubOpts', (_message.Message,), { - 'DESCRIPTOR' : _RPC_SUBOPTS, - '__module__' : 'rpc_pb2' + SubOpts = _reflection.GeneratedProtocolMessageType('SubOpts', (_message.Message,), dict( + DESCRIPTOR = _RPC_SUBOPTS, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.RPC.SubOpts) - }) + )) , - 'DESCRIPTOR' : _RPC, - '__module__' : 'rpc_pb2' + DESCRIPTOR = _RPC, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.RPC) - }) + )) _sym_db.RegisterMessage(RPC) _sym_db.RegisterMessage(RPC.SubOpts) -Message = _reflection.GeneratedProtocolMessageType('Message', (_message.Message,), { - 'DESCRIPTOR' : _MESSAGE, - '__module__' : 'rpc_pb2' +Message = _reflection.GeneratedProtocolMessageType('Message', (_message.Message,), dict( + DESCRIPTOR = _MESSAGE, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.Message) - }) + )) _sym_db.RegisterMessage(Message) -ControlMessage = _reflection.GeneratedProtocolMessageType('ControlMessage', (_message.Message,), { - 'DESCRIPTOR' : _CONTROLMESSAGE, - '__module__' : 'rpc_pb2' +ControlMessage = _reflection.GeneratedProtocolMessageType('ControlMessage', (_message.Message,), dict( + DESCRIPTOR = _CONTROLMESSAGE, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.ControlMessage) - }) + )) _sym_db.RegisterMessage(ControlMessage) -ControlIHave = _reflection.GeneratedProtocolMessageType('ControlIHave', (_message.Message,), { - 'DESCRIPTOR' : _CONTROLIHAVE, - '__module__' : 'rpc_pb2' +ControlIHave = _reflection.GeneratedProtocolMessageType('ControlIHave', (_message.Message,), dict( + DESCRIPTOR = _CONTROLIHAVE, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.ControlIHave) - }) + )) _sym_db.RegisterMessage(ControlIHave) -ControlIWant = _reflection.GeneratedProtocolMessageType('ControlIWant', (_message.Message,), { - 'DESCRIPTOR' : _CONTROLIWANT, - '__module__' : 'rpc_pb2' +ControlIWant = _reflection.GeneratedProtocolMessageType('ControlIWant', (_message.Message,), dict( + DESCRIPTOR = _CONTROLIWANT, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.ControlIWant) - }) + )) _sym_db.RegisterMessage(ControlIWant) -ControlGraft = _reflection.GeneratedProtocolMessageType('ControlGraft', (_message.Message,), { - 'DESCRIPTOR' : _CONTROLGRAFT, - '__module__' : 'rpc_pb2' +ControlGraft = _reflection.GeneratedProtocolMessageType('ControlGraft', (_message.Message,), dict( + DESCRIPTOR = _CONTROLGRAFT, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.ControlGraft) - }) + )) _sym_db.RegisterMessage(ControlGraft) -ControlPrune = _reflection.GeneratedProtocolMessageType('ControlPrune', (_message.Message,), { - 'DESCRIPTOR' : _CONTROLPRUNE, - '__module__' : 'rpc_pb2' +ControlPrune = _reflection.GeneratedProtocolMessageType('ControlPrune', (_message.Message,), dict( + DESCRIPTOR = _CONTROLPRUNE, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.ControlPrune) - }) + )) _sym_db.RegisterMessage(ControlPrune) -TopicDescriptor = _reflection.GeneratedProtocolMessageType('TopicDescriptor', (_message.Message,), { +TopicDescriptor = _reflection.GeneratedProtocolMessageType('TopicDescriptor', (_message.Message,), dict( - 'AuthOpts' : _reflection.GeneratedProtocolMessageType('AuthOpts', (_message.Message,), { - 'DESCRIPTOR' : _TOPICDESCRIPTOR_AUTHOPTS, - '__module__' : 'rpc_pb2' + AuthOpts = _reflection.GeneratedProtocolMessageType('AuthOpts', (_message.Message,), dict( + DESCRIPTOR = _TOPICDESCRIPTOR_AUTHOPTS, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.TopicDescriptor.AuthOpts) - }) + )) , - 'EncOpts' : _reflection.GeneratedProtocolMessageType('EncOpts', (_message.Message,), { - 'DESCRIPTOR' : _TOPICDESCRIPTOR_ENCOPTS, - '__module__' : 'rpc_pb2' + EncOpts = _reflection.GeneratedProtocolMessageType('EncOpts', (_message.Message,), dict( + DESCRIPTOR = _TOPICDESCRIPTOR_ENCOPTS, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.TopicDescriptor.EncOpts) - }) + )) , - 'DESCRIPTOR' : _TOPICDESCRIPTOR, - '__module__' : 'rpc_pb2' + DESCRIPTOR = _TOPICDESCRIPTOR, + __module__ = 'rpc_pb2' # @@protoc_insertion_point(class_scope:pubsub.pb.TopicDescriptor) - }) + )) _sym_db.RegisterMessage(TopicDescriptor) _sym_db.RegisterMessage(TopicDescriptor.AuthOpts) _sym_db.RegisterMessage(TopicDescriptor.EncOpts) diff --git a/setup.py b/setup.py index f91ec65..0897052 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ extras_require = { ], "lint": [ "mypy>=0.701,<1.0", + "mypy-protobuf==1.15", "black==19.3b0", "isort==4.3.21", "flake8>=3.7.7,<4.0.0",