diff --git a/tools/pb2py b/tools/pb2py index 62c2dc904..2d1a5d63d 100755 --- a/tools/pb2py +++ b/tools/pb2py @@ -100,14 +100,19 @@ def process_message(descriptor, protobuf_module, msg_id, is_upy): if is_upy: yield "import protobuf as p" + else: + yield "from .. import protobuf as p" + + fields = list(parse_field(number, field) + for number, field + in descriptor.fields_by_number.items()) + + if any(field.repeated for field in fields): yield "if __debug__:" yield " try:" yield " from typing import List" yield " except ImportError:" yield " List = None" - else: - yield "from .. import protobuf as p" - yield "from typing import List" yield from process_message_imports(descriptor) @@ -115,10 +120,6 @@ def process_message(descriptor, protobuf_module, msg_id, is_upy): yield "" yield "class %s(p.MessageType):" % descriptor.name - fields = list(parse_field(number, field) - for number, field - in descriptor.fields_by_number.items()) - if fields: yield " FIELDS = {" for field in fields: