-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoc-gen-mypy.py
executable file
·336 lines (278 loc) · 13.2 KB
/
protoc-gen-mypy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python
# Taken from https://github.com/dropbox/mypy-protobuf/blob/6abe67773910a920f943599bea9430dcb0a2cfc5/python/protoc-gen-mypy
# (Apache 2.0 License)
# with own fixes to
# - appease mypy type checker
# - fix output file names that have dashes
"""Protoc Plugin to generate mypy stubs. Loosely based on @zbarsky's go implementation"""
from __future__ import (
absolute_import,
division,
print_function,
)
import sys
from collections import defaultdict
from contextlib import contextmanager
import google.protobuf.descriptor_pb2 as d_typed # type: ignore
import six
from google.protobuf.compiler import plugin_pb2 as plugin # type: ignore
MYPY = False
if MYPY:
from typing import (
Any,
Dict,
Generator,
List,
Set,
Text,
)
# Hax to get around fact that google protobuf libraries aren't in typeshed yet
d = d_typed # type: Any
GENERATED = "@ge" + "nerated" # So phabricator doesn't think this file is generated
HEADER = "# {} by generate_proto_mypy_stubs.py. Do not edit!\n".format(GENERATED)
class PkgWriter(object):
"""Writes a single pyi file"""
def __init__(self, fd, descriptors):
# type: (d.FileDescriptorProto, Descriptors) -> None
self.fd = fd
self.descriptors = descriptors
self.lines = [] # type: List[Text]
self.indent = ""
# dictionary of x->y for `from {x} import {y}`
self.imports = defaultdict(set) # type: Dict[Text, Set[Text]]
self.locals = set() # type: Set[Text]
def _import(self, path, name):
# type: (Text, Text) -> Text
"""Imports a stdlib path and returns a handle to it
eg. self._import("typing", "Optional") -> "Optional"
"""
imp = path.replace('/', '.')
self.imports[imp].add(name)
return name
def _import_message(self, type_name):
# type: (d.FieldDescriptorProto) -> Text
"""Import a referenced message and return a handle"""
name = type_name
if name[0] == '.' and name[1].isupper():
# Message defined in this file
return name[1:] # type: ignore
message_fd = self.descriptors.message_to_fd[name]
if message_fd.name == self.fd.name:
# message defined in this package
split = type_name.split('.')
for i, segment in enumerate(split):
if segment and segment[0].isupper():
return ".".join(split[i:]) # type: ignore
# Not in package. Must import
split = type_name.split(".")
for i, segment in enumerate(split):
if segment and segment[0].isupper():
assert message_fd.name.endswith('.proto')
import_name = self._import(message_fd.name[:-6] + "_pb2", segment)
remains = ".".join(split[i + 1:])
if not remains:
return import_name
raise AssertionError("Don't support nested imports yet")
# return new_nested_import(import_name, remains)
raise AssertionError("Could not parse local name " + name)
@contextmanager # type: ignore
def _indent(self):
# type: () -> Generator
self.indent = self.indent + " "
yield
self.indent = self.indent[:-4]
def _write_line(self, line, *args):
# type: (Text, *Text) -> None
self.lines.append(self.indent + line.format(*args))
def write_enums(self, enums):
# type: (List[d.EnumDescriptorProto]) -> None
line = self._write_line
for enum in enums:
line("class {}(int):", enum.name)
with self._indent():
line("@classmethod")
line("def Name(cls, number: int) -> str: ...")
line("@classmethod")
line("def Value(cls, name: str) -> int: ...")
line("@classmethod")
line("def keys(cls) -> {}[str]: ...",
self._import("typing", "List"))
line("@classmethod")
line("def values(cls) -> {}[int]: ...",
self._import("typing", "List"))
line("@classmethod")
line("def items(cls) -> {}[{}[str, int]]: ...",
self._import("typing", "List"),
self._import("typing", "Tuple"))
for val in enum.value:
line("{} = {}({}, {})", val.name, self._import("typing", "cast"), enum.name, val.number)
line("")
def write_messages(self, messages, prefix):
# type: (List[d.DescriptorProto], Text) -> None
line = self._write_line
message_class = self._import("google.protobuf.message", "Message")
for desc in messages:
self.locals.add(desc.name)
qualified_name = prefix + desc.name
line("class {}({}):", desc.name, message_class)
with self._indent():
# Nested enums/messages
self.write_enums(desc.enum_type)
self.write_messages(desc.nested_type, qualified_name + ".")
# Scalar fields
for field in [f for f in desc.field if is_scalar(f)]:
if field.label == d.FieldDescriptorProto.LABEL_REPEATED:
container = self._import("mypy", "RepeatedScalarFieldContainer")
line("{} = ... # type: {}[{}]", field.name, container, self.python_type(field))
else:
line("{} = ... # type: {}", field.name, self.python_type(field))
line("")
# Getters for non-scalar fields
for field in [f for f in desc.field if not is_scalar(f)]:
line("@property")
if field.label == d.FieldDescriptorProto.LABEL_REPEATED:
container = self._import("mypy", "RepeatedScalarFieldContainer")
line("def {}(self) -> {}[{}]: ...", field.name, container, self.python_type(field))
else:
line("def {}(self) -> {}: ...", field.name, self.python_type(field))
line("")
# Constructor
line("def __init__(self,")
with self._indent():
# Required args
for field in [f for f in desc.field if f.label == d.FieldDescriptorProto.LABEL_REQUIRED]:
line("{} : {},", field.name, self.python_type(field))
for field in [f for f in desc.field if f.label != d.FieldDescriptorProto.LABEL_REQUIRED]:
if field.label == d.FieldDescriptorProto.LABEL_REPEATED:
line("{} : {}[{}] = None,", field.name,
self._import("typing", "Iterable"), self.python_type(field))
else:
line("{} : {} = None,", field.name, self.python_type(field))
line(") -> None: ...")
# Standard message methods
line("@classmethod")
line("def FromString(cls, s: bytes) -> {}: ...", qualified_name)
line("def MergeFrom(self, other_msg: {}) -> None: ...", message_class)
line("def CopyFrom(self, other_msg: {}) -> None: ...", message_class)
line("")
def write_services(self, services):
# type: (d.ServiceDescriptorProto) -> None
line = self._write_line
for service in services:
# The service definition interface
line("class {}({}, metaclass={}):", service.name, self._import("google.protobuf.service", "Service"), self._import("abc", "ABCMeta"))
with self._indent():
for method in service.method:
line("@{}", self._import("abc", "abstractmethod"))
line("def {}(self,", method.name)
with self._indent():
line("rpc_controller: {},", self._import("google.protobuf.service", "RpcController"))
line("request: {},", self._import_message(method.input_type))
line("done: {}[{}[[{}], None]],",
self._import("typing", "Optional"),
self._import("typing", "Callable"),
self._import_message(method.output_type))
line(") -> {}[{}]: ...", self._import("concurrent.futures", "Future"), self._import_message(method.output_type))
# The stub client
line("class {}({}):", service.name + "_Stub", service.name)
with self._indent():
line("def __init__(self, rpc_channel: {}) -> None: ...",
self._import("google.protobuf.service", "RpcChannel"))
def python_type(self, field):
# type: (d.FieldDescriptorProto) -> Text
mapping = {
d.FieldDescriptorProto.TYPE_DOUBLE: lambda: "float",
d.FieldDescriptorProto.TYPE_FLOAT: lambda: "float",
d.FieldDescriptorProto.TYPE_INT64: lambda: "int",
d.FieldDescriptorProto.TYPE_UINT64: lambda: "int",
d.FieldDescriptorProto.TYPE_FIXED64: lambda: "int",
d.FieldDescriptorProto.TYPE_SFIXED64: lambda: "int",
d.FieldDescriptorProto.TYPE_SINT64: lambda: "int",
d.FieldDescriptorProto.TYPE_INT32: lambda: "int",
d.FieldDescriptorProto.TYPE_UINT32: lambda: "int",
d.FieldDescriptorProto.TYPE_FIXED32: lambda: "int",
d.FieldDescriptorProto.TYPE_SFIXED32: lambda: "int",
d.FieldDescriptorProto.TYPE_SINT32: lambda: "int",
d.FieldDescriptorProto.TYPE_BOOL: lambda: "bool",
d.FieldDescriptorProto.TYPE_STRING: lambda: self._import("typing", "Text"),
d.FieldDescriptorProto.TYPE_BYTES: lambda: "bytes",
d.FieldDescriptorProto.TYPE_ENUM: lambda: self._import_message(field.type_name),
d.FieldDescriptorProto.TYPE_MESSAGE: lambda: self._import_message(field.type_name),
d.FieldDescriptorProto.TYPE_GROUP: lambda: self._import_message(field.type_name),
}
assert field.type in mapping, "Unrecognized type: " + field.type
return mapping[field.type]()
def write(self):
# type: () -> Text
imports = []
for pkg, items in six.iteritems(self.imports):
imports.append(u"from {} import (".format(pkg))
for item in items:
imports.append(u" {},".format(item))
imports.append(u")\n")
return "\n".join(imports + self.lines)
def is_scalar(fd):
# type: (d.FileDescriptorProto) -> bool
return not (
fd.type == d.FieldDescriptorProto.TYPE_MESSAGE or
fd.type == d.FieldDescriptorProto.TYPE_GROUP
)
def generate_mypy_stubs(descriptors, response):
# type: (Descriptors, plugin.CodeGeneratorResponse) -> plugin.CodeGeneratorResponse
for name, fd in six.iteritems(descriptors.to_generate):
pkg_writer = PkgWriter(fd, descriptors)
pkg_writer.write_enums(fd.enum_type)
pkg_writer.write_messages(fd.message_type, "")
pkg_writer.write_services(fd.service)
assert name == fd.name
assert fd.name.endswith('.proto')
output = response.file.add()
output.name = fd.name[:-6].replace('-', '_') + '_pb2.pyi'
output.content = HEADER + pkg_writer.write()
print("Writing mypy to", output.name, file=sys.stderr)
class Descriptors(object):
def __init__(self, request):
# type: (plugin.CodeGeneratorRequest) -> None
files = {f.name: f for f in request.proto_file}
to_generate = {n: files[n] for n in request.file_to_generate}
self.files = files # type: Dict[Text, d.FileDescriptorProto]
self.to_generate = to_generate # type: Dict[Text, d.FileDescriptorProto]
self.message_to_fd = {} # type: Dict[Text, d.FileDescriptorProto]
def _add_enums(enums, prefix, fd):
# type: (d.EnumDescriptorProto, Text) -> None
for enum in enums:
self.message_to_fd[prefix + enum.name] = fd
def _add_messages(messages, prefix, fd):
# type: (d.DescriptorProto, Text) -> None
for message in messages:
self.message_to_fd[prefix + message.name] = fd
sub_prefix = prefix + message.name + "."
_add_messages(message.nested_type, sub_prefix, fd)
_add_enums(message.enum_type, sub_prefix, fd)
for fd in request.proto_file:
start_prefix = "." + fd.package + "."
_add_messages(fd.message_type, start_prefix, fd)
_add_enums(fd.enum_type, start_prefix, fd)
def main():
# type: () -> None
# Read request message from stdin
if six.PY3:
data = sys.stdin.buffer.read()
else:
data = sys.stdin.read()
# Parse request
request = plugin.CodeGeneratorRequest()
request.ParseFromString(data)
# Create response
response = plugin.CodeGeneratorResponse()
# Generate mypy
generate_mypy_stubs(Descriptors(request), response)
# Serialise response message
output = response.SerializeToString()
# Write to stdout
if six.PY3:
sys.stdout.buffer.write(output)
else:
sys.stdout.write(output)
if __name__ == '__main__':
main()