Skip to content

Commit 91a4451

Browse files
committed
Update contrib/python/mypy-protobuf to 3.5.0
commit_hash:bfda51486cc75a7834db8b71f122c4f26dde8b37
1 parent 6c39e22 commit 91a4451

File tree

5 files changed

+156
-79
lines changed

5 files changed

+156
-79
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Metadata-Version: 2.1
22
Name: mypy-protobuf
3-
Version: 3.3.0
3+
Version: 3.5.0
44
Summary: Generate mypy stub files from protobuf specs
55
Home-page: https://github.com/nipunn1313/mypy-protobuf
66
Download-URL: https://github.com/nipunn1313/mypy-protobuf/releases
77
Author: Nipunn Koorapati
88
Author-email: nipunn1313@gmail.com
99
License: Apache License 2.0
1010
Keywords: mypy proto dropbox
11-
Requires-Python: >=3.7
11+
Requires-Python: >=3.8
1212
License-File: LICENSE
13-
Requires-Dist: protobuf (>=3.19.4)
14-
Requires-Dist: types-protobuf (>=3.19.12)
13+
Requires-Dist: protobuf (>=4.23.4)
14+
Requires-Dist: types-protobuf (>=4.23.0.2)
1515

contrib/python/mypy-protobuf/README.md

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,42 @@ Built originally with love at [Dropbox](https://github.com/dropbox)
1212
See [Changelog](CHANGELOG.md) for recent changes.
1313

1414
## Requirements to run mypy-protobuf
15+
1516
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.
1921

2022
## Requirements to run typecheckers on stubs generated by mypy-protobuf
23+
2124
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
2529

2630
### To run typecheckers on code generated with grpc plugin - you'll additionally need
31+
2732
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/)
3137

3238
Other configurations may work, but are not continuously tested currently.
3339
We would be open to expanding this list - file an issue on the issue tracker.
3440

3541
## Installation
3642

3743
The plugin can be installed with
44+
3845
```
3946
pip3 install mypy-protobuf
4047
```
48+
4149
To install unreleased
50+
4251
```
4352
REV=main # or whichever unreleased git rev you'd like
4453
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
4857
```
4958

5059
In order to run mypy on the generated code, you'll need to install
60+
5161
```
5262
pip3 install mypy>=0.910 types-protobuf>=0.1.14
5363
```
@@ -56,14 +66,19 @@ pip3 install mypy>=0.910 types-protobuf>=0.1.14
5666

5767
On posix, protoc-gen-mypy is installed to python's executable bin. Assuming that's
5868
on your $PATH, you can run
69+
5970
```
6071
protoc --python_out=output/location --mypy_out=output/location
6172
```
73+
6274
Alternately, you can explicitly provide the path:
75+
6376
```
6477
protoc --plugin=protoc-gen-mypy=path/to/protoc-gen-mypy --python_out=output/location --mypy_out=output/location
6578
```
79+
6680
Check the version number with
81+
6782
```
6883
> protoc-gen-mypy --version
6984
```
@@ -85,18 +100,21 @@ will appear as docstrings in .pyi files. Useful in IDEs for showing completions
85100
### Types enum int values more strongly
86101

87102
Enum int values produce stubs which wrap the int values in NewType
103+
88104
```proto
89105
enum MyEnum {
90106
HELLO = 0;
91107
WORLD = 1;
92108
}
93109
```
110+
94111
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`.
95112
This allows mypy to catch bugs where the wrong enum value is being used.
96113

97114
Calling code may be typed as follows.
98115

99116
In python >= 3.7
117+
100118
```python
101119
# May need [PEP 563](https://www.python.org/dev/peps/pep-0563/) to postpone evaluation of annotations
102120
# from __future__ import annotations # Not needed with python>=3.11 or protobuf>=3.20.0
@@ -108,11 +126,13 @@ f(MyEnum.Value("HELLO"))
108126
With protobuf <= 3.20.0, for usages of cast, the type of `x` must be quoted
109127
After protobuf >= 3.20.0 - `ValueType` exists in the python code and quotes aren't needed
110128
until [upstream protobuf](https://github.com/protocolbuffers/protobuf/pull/8182) includes `ValueType`
129+
111130
```python
112131
cast('MyEnum.ValueType', x)
113132
```
114133

115134
Similarly, for type aliases with protobuf < 3.20.0, you must either quote the type or hide it behind `TYPE_CHECKING`
135+
116136
```python
117137
from typing import Tuple, TYPE_CHECKING
118138
HELLO = Tuple['MyEnum.ValueType', 'MyEnum.ValueType']
@@ -122,7 +142,7 @@ if TYPE_CHECKING:
122142

123143
#### Enum int impl details
124144

125-
mypy-protobuf autogenerates an instance of the EnumTypeWrapper as follows.
145+
mypy-protobuf autogenerates an instance of the EnumTypeWrapper as follows.
126146

127147
```python
128148
class _MyEnum:
@@ -141,14 +161,14 @@ WORLD: MyEnum.ValueType # 1
141161

142162
`_MyEnumEnumTypeWrapper` extends the EnumTypeWrapper to take/return MyEnum.ValueType rather than int
143163
`MyEnum` is an instance of the `EnumTypeWrapper`.
164+
144165
- Use `_MyEnum` and of metaclass is an implementation detail to make MyEnum.ValueType a valid type w/o a circular dependency
145166
- `V` is supported as an alias of `ValueType` for backward compatibility
146167

147-
148-
149168
### Supports generating type wrappers for fields and maps
150169

151170
M.proto
171+
152172
```proto
153173
message M {
154174
uint32 user_id = 1 [(mypy_protobuf.options).casttype="mymod.UserId"];
@@ -158,24 +178,29 @@ message M {
158178
];
159179
}
160180
```
181+
161182
mymod.py
183+
162184
```python
163185
UserId = NewType("UserId", int)
164186
Email = NewType("Email", Text)
165187
```
166188

167189
### `py_generic_services`
190+
168191
If `py_generic_services` is set in your proto file, then mypy-protobuf will
169192
generate service stubs. If you want GRPC stubs instead - use the GRPC instructions.
170193

171194
### `readable_stubs`
195+
172196
If `readable_stubs` is set, mypy-protobuf will generate easier-to-read stubs. The downside
173197
to this approach - is that it's possible to generate stubs which do not pass mypy - particularly
174198
in the case of name collisions. mypy-protobuf defaults to generating stubs with fully qualified
175199
imports and mangled global-level identifiers to defend against name collisions between global
176200
identifiers and field names.
177201

178202
If you're ok with this risk, try it out!
203+
179204
```
180205
protoc --python_out=output/location --mypy_out=readable_stubs:output/location
181206
```
@@ -195,14 +220,17 @@ protoc --python_out=output/location --mypy_out=relax_strict_optional_primitives:
195220
```
196221

197222
### Output suppression
223+
198224
To suppress output, you can run
225+
199226
```
200227
protoc --python_out=output/location --mypy_out=quiet:output/location
201228
```
202229

203230
### GRPC
204231

205232
This plugin provides stubs generation for grpcio generated code.
233+
206234
```
207235
protoc \
208236
--python_out=output/location \
@@ -212,21 +240,23 @@ protoc \
212240
```
213241

214242
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
216244

217245
### Targeting python2 support
218246

219247
mypy-protobuf's drops support for targeting python2 with version 3.0. If you still need python2 support -
248+
220249
```
221250
python3 -m pip install mypy_protobuf==2.10
222251
protoc --python_out=output/location --mypy_out=output/location
223252
mypy --target-version=2.7 {files}
224253
```
225254

226-
227255
## Contributing
256+
228257
Contributions to the implementation are welcome. Please run tests using `./run_test.sh`.
229258
Ensure code is formatted using black.
259+
230260
```
231261
pip3 install black
232262
black .
@@ -271,9 +301,9 @@ black .
271301
- [@miaachan](https://github.com/miaachan)
272302
- [@Alphadelta14](https://github.com/Alphadelta14)
273303
- [@fergyfresh](https://github.com/fergyfresh)
304+
- [@AlexWaygood](https://github.com/AlexWaygood)
274305

275-
Licence etc.
276-
------------
306+
## Licence etc.
277307

278308
1. License: Apache 2.0.
279-
2. Copyright attribution: Copyright (c) 2017 Dropbox, Inc.
309+
2. Copyright attribution: Copyright (c) 2022 Nipunn Koorapati

contrib/python/mypy-protobuf/mypy_protobuf/extensions_pb2.py

Lines changed: 7 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)