@@ -12,33 +12,42 @@ Built originally with love at [Dropbox](https://github.com/dropbox)
12
12
See [ Changelog] ( CHANGELOG.md ) for recent changes.
13
13
14
14
## Requirements to run mypy-protobuf
15
+
15
16
Earlier releases might work, but aren't tested
16
- - [ protoc >= 3.19.4] ( https://github.com/protocolbuffers/protobuf/releases )
17
- - [ python-protobuf >= 3.19.4] ( https://pypi.org/project/protobuf/ ) - matching protoc release
18
- - [ python >= 3.7] ( https://www.python.org/downloads/source/ ) - for running mypy-protobuf plugin.
17
+
18
+ - [ protoc >= 23.4] ( https://github.com/protocolbuffers/protobuf/releases )
19
+ - [ python-protobuf >= 4.23.4] ( https://pypi.org/project/protobuf/ ) - matching protoc release
20
+ - [ python >= 3.8] ( https://www.python.org/downloads/source/ ) - for running mypy-protobuf plugin.
19
21
20
22
## Requirements to run typecheckers on stubs generated by mypy-protobuf
23
+
21
24
Earlier releases might work, but aren't tested
22
- - [ mypy >= v0.941] ( https://pypi.org/project/mypy ) or [ pyright >= 1.1.206] ( https://github.com/microsoft/pyright )
23
- - [ python-protobuf >= 3.19.4] ( https://pypi.org/project/protobuf/ ) - matching protoc release
24
- - [ types-protobuf >= 3.19.12] ( https://pypi.org/project/types-protobuf/ ) - for stubs from the google.protobuf library
25
+
26
+ - [ mypy >= v1.4.1] ( https://pypi.org/project/mypy ) or [ pyright >= 1.1.206] ( https://github.com/microsoft/pyright )
27
+ - [ python-protobuf >= 4.23.4] ( https://pypi.org/project/protobuf/ ) - matching protoc release
28
+ - [ types-protobuf >= 4.23.0.2] ( https://pypi.org/project/types-protobuf/ ) - for stubs from the google.protobuf library
25
29
26
30
### To run typecheckers on code generated with grpc plugin - you'll additionally need
31
+
27
32
Earlier releases might work, but aren't tested
28
- - [ grpcio>=1.47.0] ( https://pypi.org/project/grpcio/ )
29
- - [ grpcio-tools>=1.47.0] ( https://pypi.org/project/grpcio-tools/ )
30
- - [ grpc-stubs>=1.24.9] ( https://pypi.org/project/grpc-stubs/ )
33
+
34
+ - [ grpcio>=1.56.2] ( https://pypi.org/project/grpcio/ )
35
+ - [ grpcio-tools>=1.56.2] ( https://pypi.org/project/grpcio-tools/ )
36
+ - [ grpc-stubs>=1.53.0.2] ( https://pypi.org/project/grpc-stubs/ )
31
37
32
38
Other configurations may work, but are not continuously tested currently.
33
39
We would be open to expanding this list - file an issue on the issue tracker.
34
40
35
41
## Installation
36
42
37
43
The plugin can be installed with
44
+
38
45
```
39
46
pip3 install mypy-protobuf
40
47
```
48
+
41
49
To install unreleased
50
+
42
51
```
43
52
REV=main # or whichever unreleased git rev you'd like
44
53
pip3 install git+https://github.com/nipunn1313/mypy-protobuf.git@$REV
@@ -48,6 +57,7 @@ pip3 install git+https://github.com/nipunn1313/mypy-protobuf.git@$REV#subdirecto
48
57
```
49
58
50
59
In order to run mypy on the generated code, you'll need to install
60
+
51
61
```
52
62
pip3 install mypy>=0.910 types-protobuf>=0.1.14
53
63
```
@@ -56,14 +66,19 @@ pip3 install mypy>=0.910 types-protobuf>=0.1.14
56
66
57
67
On posix, protoc-gen-mypy is installed to python's executable bin. Assuming that's
58
68
on your $PATH, you can run
69
+
59
70
```
60
71
protoc --python_out=output/location --mypy_out=output/location
61
72
```
73
+
62
74
Alternately, you can explicitly provide the path:
75
+
63
76
```
64
77
protoc --plugin=protoc-gen-mypy=path/to/protoc-gen-mypy --python_out=output/location --mypy_out=output/location
65
78
```
79
+
66
80
Check the version number with
81
+
67
82
```
68
83
> protoc-gen-mypy --version
69
84
```
@@ -85,18 +100,21 @@ will appear as docstrings in .pyi files. Useful in IDEs for showing completions
85
100
### Types enum int values more strongly
86
101
87
102
Enum int values produce stubs which wrap the int values in NewType
103
+
88
104
``` proto
89
105
enum MyEnum {
90
106
HELLO = 0;
91
107
WORLD = 1;
92
108
}
93
109
```
110
+
94
111
Will yield an [ enum type wrapper] ( https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stubs/protobuf/google/protobuf/internal/enum_type_wrapper.pyi ) whose methods type to ` MyEnum.ValueType ` (a ` NewType(int) ` rather than ` int ` .
95
112
This allows mypy to catch bugs where the wrong enum value is being used.
96
113
97
114
Calling code may be typed as follows.
98
115
99
116
In python >= 3.7
117
+
100
118
``` python
101
119
# May need [PEP 563](https://www.python.org/dev/peps/pep-0563/) to postpone evaluation of annotations
102
120
# from __future__ import annotations # Not needed with python>=3.11 or protobuf>=3.20.0
@@ -108,11 +126,13 @@ f(MyEnum.Value("HELLO"))
108
126
With protobuf <= 3.20.0, for usages of cast, the type of ` x ` must be quoted
109
127
After protobuf >= 3.20.0 - ` ValueType ` exists in the python code and quotes aren't needed
110
128
until [ upstream protobuf] ( https://github.com/protocolbuffers/protobuf/pull/8182 ) includes ` ValueType `
129
+
111
130
``` python
112
131
cast(' MyEnum.ValueType' , x)
113
132
```
114
133
115
134
Similarly, for type aliases with protobuf < 3.20.0, you must either quote the type or hide it behind ` TYPE_CHECKING `
135
+
116
136
``` python
117
137
from typing import Tuple, TYPE_CHECKING
118
138
HELLO = Tuple[' MyEnum.ValueType' , ' MyEnum.ValueType' ]
@@ -122,7 +142,7 @@ if TYPE_CHECKING:
122
142
123
143
#### Enum int impl details
124
144
125
- mypy-protobuf autogenerates an instance of the EnumTypeWrapper as follows.
145
+ mypy-protobuf autogenerates an instance of the EnumTypeWrapper as follows.
126
146
127
147
``` python
128
148
class _MyEnum :
@@ -141,14 +161,14 @@ WORLD: MyEnum.ValueType # 1
141
161
142
162
` _MyEnumEnumTypeWrapper ` extends the EnumTypeWrapper to take/return MyEnum.ValueType rather than int
143
163
` MyEnum ` is an instance of the ` EnumTypeWrapper ` .
164
+
144
165
- Use ` _MyEnum ` and of metaclass is an implementation detail to make MyEnum.ValueType a valid type w/o a circular dependency
145
166
- ` V ` is supported as an alias of ` ValueType ` for backward compatibility
146
167
147
-
148
-
149
168
### Supports generating type wrappers for fields and maps
150
169
151
170
M.proto
171
+
152
172
``` proto
153
173
message M {
154
174
uint32 user_id = 1 [(mypy_protobuf.options).casttype="mymod.UserId"];
@@ -158,24 +178,29 @@ message M {
158
178
];
159
179
}
160
180
```
181
+
161
182
mymod.py
183
+
162
184
``` python
163
185
UserId = NewType(" UserId" , int )
164
186
Email = NewType(" Email" , Text)
165
187
```
166
188
167
189
### ` py_generic_services `
190
+
168
191
If ` py_generic_services ` is set in your proto file, then mypy-protobuf will
169
192
generate service stubs. If you want GRPC stubs instead - use the GRPC instructions.
170
193
171
194
### ` readable_stubs `
195
+
172
196
If ` readable_stubs ` is set, mypy-protobuf will generate easier-to-read stubs. The downside
173
197
to this approach - is that it's possible to generate stubs which do not pass mypy - particularly
174
198
in the case of name collisions. mypy-protobuf defaults to generating stubs with fully qualified
175
199
imports and mangled global-level identifiers to defend against name collisions between global
176
200
identifiers and field names.
177
201
178
202
If you're ok with this risk, try it out!
203
+
179
204
```
180
205
protoc --python_out=output/location --mypy_out=readable_stubs:output/location
181
206
```
@@ -195,14 +220,17 @@ protoc --python_out=output/location --mypy_out=relax_strict_optional_primitives:
195
220
```
196
221
197
222
### Output suppression
223
+
198
224
To suppress output, you can run
225
+
199
226
```
200
227
protoc --python_out=output/location --mypy_out=quiet:output/location
201
228
```
202
229
203
230
### GRPC
204
231
205
232
This plugin provides stubs generation for grpcio generated code.
233
+
206
234
```
207
235
protoc \
208
236
--python_out=output/location \
@@ -212,21 +240,23 @@ protoc \
212
240
```
213
241
214
242
Note that generated code for grpc will work only together with code for python and locations should be the same.
215
- If you need stubs for grpc internal code we suggest using this package https://github.com/shabbyrobe/grpc-stubs
243
+ If you need stubs for grpc internal code we suggest using this package https://github.com/shabbyrobe/grpc-stubs
216
244
217
245
### Targeting python2 support
218
246
219
247
mypy-protobuf's drops support for targeting python2 with version 3.0. If you still need python2 support -
248
+
220
249
```
221
250
python3 -m pip install mypy_protobuf==2.10
222
251
protoc --python_out=output/location --mypy_out=output/location
223
252
mypy --target-version=2.7 {files}
224
253
```
225
254
226
-
227
255
## Contributing
256
+
228
257
Contributions to the implementation are welcome. Please run tests using ` ./run_test.sh ` .
229
258
Ensure code is formatted using black.
259
+
230
260
```
231
261
pip3 install black
232
262
black .
@@ -271,9 +301,9 @@ black .
271
301
- [ @miaachan ] ( https://github.com/miaachan )
272
302
- [ @Alphadelta14 ] ( https://github.com/Alphadelta14 )
273
303
- [ @fergyfresh ] ( https://github.com/fergyfresh )
304
+ - [ @AlexWaygood ] ( https://github.com/AlexWaygood )
274
305
275
- Licence etc.
276
- ------------
306
+ ## Licence etc.
277
307
278
308
1 . License: Apache 2.0.
279
- 2 . Copyright attribution: Copyright (c) 2017 Dropbox, Inc.
309
+ 2 . Copyright attribution: Copyright (c) 2022 Nipunn Koorapati
0 commit comments