Skip to content

Commit b0a97c4

Browse files
authored
Upgrade to python-protobuf 3.7 (#77)
1 parent 00d7081 commit b0a97c4

16 files changed

+78
-82
lines changed

proto/test/proto/test.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ message Simple2 {
4949
}
5050

5151
option py_generic_services = true;
52-
service TestService {
52+
service ATestService {
5353
rpc Echo(Simple1) returns (Simple2) {}
5454
}

proto/test/proto/test_no_generic_services.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ message Simple3 {
66
required string a_string = 1;
77
}
88

9-
service TestService2 {
9+
service ATestService2 {
1010
rpc Echo(Simple3) returns (Simple3) {}
1111
}

python/protoc-gen-mypy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class PkgWriter(object):
215215
"""Type the stringly-typed methods as a Union[Literal, Literal ...]"""
216216
l = self._write_line
217217
# HasField accepts bytes/unicode in PY2, but only unicode in PY3
218-
# ClearField accepts bytes in PY2 and unicode in PY3
218+
# ClearField accepts bytes/unicode in PY2 and unicode in PY3
219219
# WhichOneof accepts bytes/unicode in both PY2 and PY3
220220
#
221221
# HasField only supports singular. ClearField supports repeated as well
@@ -241,7 +241,7 @@ class PkgWriter(object):
241241
hf_fields_py3 = ",".join(sorted('u"{}"'.format(name) for name in hf_fields))
242242
cf_fields_py3 = ",".join(sorted('u"{}"'.format(name) for name in cf_fields))
243243
hf_fields_py2 = ",".join(sorted('u"{}",b"{}"'.format(name, name) for name in hf_fields))
244-
cf_fields_py2 = ",".join(sorted('b"{}"'.format(name, name) for name in cf_fields))
244+
cf_fields_py2 = ",".join(sorted('u"{}",b"{}"'.format(name, name) for name in cf_fields))
245245

246246
if not hf_fields and not cf_fields and not wo_fields:
247247
return

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
protobuf==3.6.1
1+
protobuf==3.7.1
22
six
33
pytest
44
typing

run_test.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#!/bin/bash -ex
22

3+
PY_VERSION=`python -c 'import sys; print(sys.version.split()[0])'`
4+
VENV=venv_$PY_VERSION
5+
MYPY_VENV=venv_mypy
6+
37
(
48
# Create virtualenv + Install requirements for mypy-protobuf
5-
if [[ -z $SKIP_CLEAN ]] || [[ ! -e env ]]; then
6-
python -m virtualenv env
9+
if [[ -z $SKIP_CLEAN ]] || [[ ! -e $VENV ]]; then
10+
python -m virtualenv $VENV
711
fi
8-
source env/bin/activate
12+
source $VENV/bin/activate
913
python -m pip install -r requirements.txt
1014

1115
# Generate protos
@@ -14,15 +18,15 @@
1418
)
1519

1620
(
17-
# Run mypy
21+
# Run mypy (always under python3)
1822

1923
# Create virtualenv
20-
if [[ -z $SKIP_CLEAN ]] || [[ ! -e mypy_env ]]; then
24+
if [[ -z $SKIP_CLEAN ]] || [[ ! -e $MYPY_VENV ]]; then
2125
python3 --version
22-
python3 -m virtualenv mypy_env
26+
python3 -m virtualenv $MYPY_VENV
2327
fi
24-
source mypy_env/bin/activate
25-
if [[ -z $SKIP_CLEAN ]] || [[ ! -e mypy_env ]]; then
28+
source $MYPY_VENV/bin/activate
29+
if [[ -z $SKIP_CLEAN ]] || [[ ! -e $MYPY_VENV ]]; then
2630
python3 -m pip install setuptools
2731
python3 -m pip install git+https://github.com/python/[email protected]
2832
fi
@@ -42,7 +46,7 @@
4246

4347
(
4448
# Run unit tests. These tests generate .expected files
45-
source env/bin/activate
49+
source $VENV/bin/activate
4650
python --version
4751
py.test --version
4852
py.test

test/proto/Capitalized/Capitalized_pb2.pyi.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class lower(google___protobuf___message___Message):
2727
if sys.version_info >= (3,):
2828
def ClearField(self, field_name: typing_extensions___Literal[u"a"]) -> None: ...
2929
else:
30-
def ClearField(self, field_name: typing_extensions___Literal[b"a"]) -> None: ...
30+
def ClearField(self, field_name: typing_extensions___Literal[u"a",b"a"]) -> None: ...
3131

3232
class Upper(google___protobuf___message___Message):
3333

@@ -47,7 +47,7 @@ class Upper(google___protobuf___message___Message):
4747
def ClearField(self, field_name: typing_extensions___Literal[u"Lower"]) -> None: ...
4848
else:
4949
def HasField(self, field_name: typing_extensions___Literal[u"Lower",b"Lower"]) -> bool: ...
50-
def ClearField(self, field_name: typing_extensions___Literal[b"Lower"]) -> None: ...
50+
def ClearField(self, field_name: typing_extensions___Literal[u"Lower",b"Lower"]) -> None: ...
5151

5252
class lower2(google___protobuf___message___Message):
5353

@@ -67,4 +67,4 @@ class lower2(google___protobuf___message___Message):
6767
def ClearField(self, field_name: typing_extensions___Literal[u"upper"]) -> None: ...
6868
else:
6969
def HasField(self, field_name: typing_extensions___Literal[u"upper",b"upper"]) -> bool: ...
70-
def ClearField(self, field_name: typing_extensions___Literal[b"upper"]) -> None: ...
70+
def ClearField(self, field_name: typing_extensions___Literal[u"upper",b"upper"]) -> None: ...

test/proto/inner/inner_pb2.pyi.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ class Inner(google___protobuf___message___Message):
3131
if sys.version_info >= (3,):
3232
def ClearField(self, field_name: typing_extensions___Literal[u"a"]) -> None: ...
3333
else:
34-
def ClearField(self, field_name: typing_extensions___Literal[b"a"]) -> None: ...
34+
def ClearField(self, field_name: typing_extensions___Literal[u"a",b"a"]) -> None: ...

test/proto/nested/nested_pb2.pyi.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Nested(google___protobuf___message___Message):
3939
if sys.version_info >= (3,):
4040
def ClearField(self, field_name: typing_extensions___Literal[u"a"]) -> None: ...
4141
else:
42-
def ClearField(self, field_name: typing_extensions___Literal[b"a"]) -> None: ...
42+
def ClearField(self, field_name: typing_extensions___Literal[u"a",b"a"]) -> None: ...
4343

4444
class AnotherNested(google___protobuf___message___Message):
4545
class NestedEnum(int):
@@ -94,7 +94,7 @@ class AnotherNested(google___protobuf___message___Message):
9494
if sys.version_info >= (3,):
9595
def ClearField(self, field_name: typing_extensions___Literal[u"b",u"ne",u"ne2",u"s"]) -> None: ...
9696
else:
97-
def ClearField(self, field_name: typing_extensions___Literal[b"b",b"ne",b"ne2",b"s"]) -> None: ...
97+
def ClearField(self, field_name: typing_extensions___Literal[u"b",b"b",u"ne",b"ne",u"ne2",b"ne2",u"s",b"s"]) -> None: ...
9898

9999

100100
def __init__(self,

test/proto/nopackage_pb2.pyi.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ class NoPackage2(google___protobuf___message___Message):
4949
def ClearField(self, field_name: typing_extensions___Literal[u"np",u"np_rep"]) -> None: ...
5050
else:
5151
def HasField(self, field_name: typing_extensions___Literal[u"np",b"np"]) -> bool: ...
52-
def ClearField(self, field_name: typing_extensions___Literal[b"np",b"np_rep"]) -> None: ...
52+
def ClearField(self, field_name: typing_extensions___Literal[u"np",b"np",u"np_rep",b"np_rep"]) -> None: ...

test/proto/test3_pb2.pyi.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class OuterMessage3(google___protobuf___message___Message):
5757
if sys.version_info >= (3,):
5858
def ClearField(self, field_name: typing_extensions___Literal[u"a_bool"]) -> None: ...
5959
else:
60-
def ClearField(self, field_name: typing_extensions___Literal[b"a_bool"]) -> None: ...
60+
def ClearField(self, field_name: typing_extensions___Literal[u"a_bool",b"a_bool"]) -> None: ...
6161

6262
class SimpleProto3(google___protobuf___message___Message):
6363
a_string = ... # type: typing___Text
@@ -91,7 +91,7 @@ class SimpleProto3(google___protobuf___message___Message):
9191
def ClearField(self, field_name: typing_extensions___Literal[u"a_oneof",u"a_oneof_1",u"a_oneof_2",u"a_repeated_string",u"a_string",u"b_oneof",u"b_oneof_1",u"b_oneof_2",u"outer_enum",u"outer_message"]) -> None: ...
9292
else:
9393
def HasField(self, field_name: typing_extensions___Literal[u"a_oneof",b"a_oneof",u"a_oneof_1",b"a_oneof_1",u"a_oneof_2",b"a_oneof_2",u"b_oneof",b"b_oneof",u"b_oneof_1",b"b_oneof_1",u"b_oneof_2",b"b_oneof_2",u"outer_message",b"outer_message"]) -> bool: ...
94-
def ClearField(self, field_name: typing_extensions___Literal[b"a_oneof",b"a_oneof_1",b"a_oneof_2",b"a_repeated_string",b"a_string",b"b_oneof",b"b_oneof_1",b"b_oneof_2",b"outer_enum",b"outer_message"]) -> None: ...
94+
def ClearField(self, field_name: typing_extensions___Literal[u"a_oneof",b"a_oneof",u"a_oneof_1",b"a_oneof_1",u"a_oneof_2",b"a_oneof_2",u"a_repeated_string",b"a_repeated_string",u"a_string",b"a_string",u"b_oneof",b"b_oneof",u"b_oneof_1",b"b_oneof_1",u"b_oneof_2",b"b_oneof_2",u"outer_enum",b"outer_enum",u"outer_message",b"outer_message"]) -> None: ...
9595
@typing___overload
9696
def WhichOneof(self, oneof_group: typing_extensions___Literal[u"a_oneof",b"a_oneof"]) -> typing_extensions___Literal["a_oneof_1","a_oneof_2"]: ...
9797
@typing___overload

test/proto/test_no_generic_services_pb2.pyi.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ class Simple3(google___protobuf___message___Message):
2929
def ClearField(self, field_name: typing_extensions___Literal[u"a_string"]) -> None: ...
3030
else:
3131
def HasField(self, field_name: typing_extensions___Literal[u"a_string",b"a_string"]) -> bool: ...
32-
def ClearField(self, field_name: typing_extensions___Literal[b"a_string"]) -> None: ...
32+
def ClearField(self, field_name: typing_extensions___Literal[u"a_string",b"a_string"]) -> None: ...

test/proto/test_pb2.pyi.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Simple1(google___protobuf___message___Message):
159159
def ClearField(self, field_name: typing_extensions___Literal[u"a_boolean",u"a_enum",u"a_external_enum",u"a_inner",u"a_nested",u"a_oneof",u"a_oneof_1",u"a_oneof_2",u"a_repeated_string",u"a_string",u"a_uint32",u"inner_enum",u"inner_message",u"nested_enum",u"nested_message",u"no_package",u"rep_inner_enum",u"rep_inner_message"]) -> None: ...
160160
else:
161161
def HasField(self, field_name: typing_extensions___Literal[u"a_boolean",b"a_boolean",u"a_enum",b"a_enum",u"a_external_enum",b"a_external_enum",u"a_inner",b"a_inner",u"a_nested",b"a_nested",u"a_oneof",b"a_oneof",u"a_oneof_1",b"a_oneof_1",u"a_oneof_2",b"a_oneof_2",u"a_string",b"a_string",u"a_uint32",b"a_uint32",u"inner_enum",b"inner_enum",u"inner_message",b"inner_message",u"nested_enum",b"nested_enum",u"nested_message",b"nested_message",u"no_package",b"no_package"]) -> bool: ...
162-
def ClearField(self, field_name: typing_extensions___Literal[b"a_boolean",b"a_enum",b"a_external_enum",b"a_inner",b"a_nested",b"a_oneof",b"a_oneof_1",b"a_oneof_2",b"a_repeated_string",b"a_string",b"a_uint32",b"inner_enum",b"inner_message",b"nested_enum",b"nested_message",b"no_package",b"rep_inner_enum",b"rep_inner_message"]) -> None: ...
162+
def ClearField(self, field_name: typing_extensions___Literal[u"a_boolean",b"a_boolean",u"a_enum",b"a_enum",u"a_external_enum",b"a_external_enum",u"a_inner",b"a_inner",u"a_nested",b"a_nested",u"a_oneof",b"a_oneof",u"a_oneof_1",b"a_oneof_1",u"a_oneof_2",b"a_oneof_2",u"a_repeated_string",b"a_repeated_string",u"a_string",b"a_string",u"a_uint32",b"a_uint32",u"inner_enum",b"inner_enum",u"inner_message",b"inner_message",u"nested_enum",b"nested_enum",u"nested_message",b"nested_message",u"no_package",b"no_package",u"rep_inner_enum",b"rep_inner_enum",u"rep_inner_message",b"rep_inner_message"]) -> None: ...
163163
def WhichOneof(self, oneof_group: typing_extensions___Literal[u"a_oneof",b"a_oneof"]) -> typing_extensions___Literal["a_oneof_1","a_oneof_2"]: ...
164164

165165
class Simple2(google___protobuf___message___Message):
@@ -178,16 +178,16 @@ class Simple2(google___protobuf___message___Message):
178178
def ClearField(self, field_name: typing_extensions___Literal[u"a_string"]) -> None: ...
179179
else:
180180
def HasField(self, field_name: typing_extensions___Literal[u"a_string",b"a_string"]) -> bool: ...
181-
def ClearField(self, field_name: typing_extensions___Literal[b"a_string"]) -> None: ...
181+
def ClearField(self, field_name: typing_extensions___Literal[u"a_string",b"a_string"]) -> None: ...
182182

183-
class TestService(google___protobuf___service___Service, metaclass=abc___ABCMeta):
183+
class ATestService(google___protobuf___service___Service, metaclass=abc___ABCMeta):
184184
@abc___abstractmethod
185185
def Echo(self,
186186
rpc_controller: google___protobuf___service___RpcController,
187187
request: Simple1,
188188
done: typing___Optional[typing___Callable[[Simple2], None]],
189189
) -> concurrent___futures___Future[Simple2]: ...
190-
class TestService_Stub(TestService):
190+
class ATestService_Stub(ATestService):
191191
def __init__(self, rpc_channel: google___protobuf___service___RpcChannel) -> None: ...
192192
def Echo(self,
193193
rpc_controller: google___protobuf___service___RpcController,

test/test_generated_mypy.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_generate_negative_matches():
6868
assert errors_35 == expected_errors_35
6969

7070
# Some sanity checks to make sure we don't mess this up. Please update as necessary.
71-
assert len(errors_27) == 21
71+
assert len(errors_27) == 19
7272
assert len(errors_35) == 23
7373

7474
def test_func():
@@ -106,16 +106,18 @@ def test_has_field_proto2():
106106
# Proto2 tests
107107
assert s.HasField(u"a_string")
108108
assert s.HasField("a_string")
109+
if six.PY2:
110+
assert s.HasField(b"a_string")
109111
assert not s.HasField("a_inner")
110112
assert not s.HasField("a_enum")
111113
assert not s.HasField("a_oneof")
112114

113115
# Erase the types to verify that incorrect inputs fail at runtime
114116
# Each test here should be duplicated in test_negative to ensure mypy fails it too
115117
s_untyped = s # type: Any
116-
with pytest.raises(ValueError, match="Unknown field garbage."):
118+
with pytest.raises(ValueError, match="Protocol message Simple1 has no field garbage."):
117119
s_untyped.HasField("garbage")
118-
with pytest.raises(ValueError, match='Protocol message has no singular "a_repeated_string" field'):
120+
with pytest.raises(ValueError, match='Protocol message Simple1 has no singular "a_repeated_string" field'):
119121
s_untyped.HasField("a_repeated_string")
120122
if six.PY3:
121123
with pytest.raises(TypeError, match='bad argument type for built-in operation'):
@@ -126,18 +128,20 @@ def test_has_field_proto3():
126128
s = SimpleProto3()
127129
assert not s.HasField(u"outer_message")
128130
assert not s.HasField("outer_message")
131+
if six.PY2:
132+
assert not s.HasField(b"outer_message")
129133
assert not s.HasField("a_oneof")
130134

131135
# Erase the types to verify that incorrect inputs fail at runtime
132136
# Each test here should be duplicated in test_negative to ensure mypy fails it too
133137
s_untyped = s # type: Any
134-
with pytest.raises(ValueError, match="Unknown field garbage."):
138+
with pytest.raises(ValueError, match="Protocol message SimpleProto3 has no field garbage."):
135139
s_untyped.HasField(u"garbage")
136-
with pytest.raises(ValueError, match='Can\'t test non-submessage field "a_string" for presence in proto3.'):
140+
with pytest.raises(ValueError, match='Can\'t test non-submessage field "SimpleProto3.a_string" for presence in proto3.'):
137141
s_untyped.HasField(u"a_string")
138-
with pytest.raises(ValueError, match='Can\'t test non-submessage field "outer_enum" for presence in proto3.'):
142+
with pytest.raises(ValueError, match='Can\'t test non-submessage field "SimpleProto3.outer_enum" for presence in proto3.'):
139143
s_untyped.HasField("outer_enum")
140-
with pytest.raises(ValueError, match='Protocol message has no singular "a_repeated_string" field'):
144+
with pytest.raises(ValueError, match='Protocol message SimpleProto3 has no singular "a_repeated_string" field'):
141145
s_untyped.HasField(u"a_repeated_string")
142146
if six.PY3:
143147
with pytest.raises(TypeError, match='bad argument type for built-in operation'):
@@ -150,9 +154,8 @@ def test_clear_field_proto2():
150154
s.a_string = "Hello"
151155

152156
# Proto2 tests
153-
if six.PY3:
154-
s.ClearField(u"a_string")
155-
else:
157+
s.ClearField(u"a_string")
158+
if six.PY2:
156159
s.ClearField(b"a_string")
157160
s.ClearField("a_string")
158161
s.ClearField("a_inner")
@@ -165,10 +168,8 @@ def test_clear_field_proto2():
165168
with pytest.raises(ValueError, match='Protocol message has no "garbage" field.'):
166169
s_untyped.ClearField("garbage")
167170
# This error message is very inconsistent w/ how HasField works
168-
with pytest.raises(TypeError, match='field name must be a string'):
169-
if six.PY2:
170-
s_untyped.ClearField(u"a_string")
171-
else:
171+
if six.PY3:
172+
with pytest.raises(TypeError, match='field name must be a string'):
172173
s_untyped.ClearField(b"a_string")
173174

174175
def test_clear_field_proto3():
@@ -178,9 +179,8 @@ def test_clear_field_proto3():
178179
s.a_string = "Hello"
179180

180181
# Proto2 tests
181-
if six.PY3:
182-
s.ClearField(u"a_string")
183-
else:
182+
s.ClearField(u"a_string")
183+
if six.PY2:
184184
s.ClearField(b"a_string")
185185
s.ClearField("a_string")
186186
s.ClearField("outer_enum")
@@ -194,10 +194,8 @@ def test_clear_field_proto3():
194194
with pytest.raises(ValueError, match='Protocol message has no "garbage" field.'):
195195
s_untyped.ClearField("garbage")
196196
# This error message is very inconsistent w/ how HasField works
197-
with pytest.raises(TypeError, match='field name must be a string'):
198-
if six.PY2:
199-
s_untyped.ClearField(u"a_string")
200-
else:
197+
if six.PY3:
198+
with pytest.raises(TypeError, match='field name must be a string'):
201199
s_untyped.ClearField(b"a_string")
202200

203201
def test_which_oneof_proto2():

test_negative/negative.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@
4949
# Proto2
5050
s.ClearField("garbage") # E:2.7 E:3.5
5151
# This error message is very inconsistent w/ how HasField works
52-
if six.PY2:
53-
s.ClearField(u"a_string") # E:2.7
54-
else:
52+
if six.PY3:
5553
s.ClearField(b"a_string") # E:3.5
5654

5755
# Proto3
5856
s6.ClearField("garbage") # E:2.7 E:3.5
5957
# This error message is very inconsistent w/ how HasField works
60-
if six.PY2:
61-
s6.ClearField(u"a_string") # E:2.7
62-
else:
58+
if six.PY3:
6359
s6.ClearField(b"a_string") # E:3.5
6460

6561
# Proto2 WhichOneof

0 commit comments

Comments
 (0)