Skip to content

Commit e912b45

Browse files
authored
fix: adding enums to a repeated field does not raise a TypeError (#202)
Fixes issue #201, where enums added to a repeated field triggered a TypeError because they were coverted to integers during marshaling.
1 parent daad511 commit e912b45

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

packages/proto-plus/proto/marshal/collections/repeated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,5 @@ def __setitem__(self, key, value):
174174

175175
def insert(self, index: int, value):
176176
"""Insert ``value`` in the sequence before ``index``."""
177-
pb_value = self._marshal.to_proto(self._pb_type, value, strict=True)
177+
pb_value = self._marshal.to_proto(self._pb_type, value)
178178
self.pb.insert(index, pb_value)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (C) 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import proto
16+
from proto.marshal.marshal import BaseMarshal
17+
import pytest
18+
19+
20+
def test_strict_to_proto():
21+
m = BaseMarshal()
22+
23+
with pytest.raises(TypeError):
24+
m.to_proto(dict, None, strict=True)

packages/proto-plus/tests/test_marshal_types_enum.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,35 @@ class Foo(proto.Enum):
5858
with mock.patch.object(warnings, "warn") as warn:
5959
assert enum_rule.to_python(4) == 4
6060
warn.assert_called_once_with("Unrecognized Foo enum value: 4")
61+
62+
63+
def test_enum_append():
64+
class Bivalve(proto.Enum):
65+
CLAM = 0
66+
OYSTER = 1
67+
68+
class MolluscContainer(proto.Message):
69+
bivalves = proto.RepeatedField(proto.ENUM, number=1, enum=Bivalve,)
70+
71+
mc = MolluscContainer()
72+
clam = Bivalve.CLAM
73+
mc.bivalves.append(clam)
74+
mc.bivalves.append(1)
75+
76+
assert mc.bivalves == [clam, Bivalve.OYSTER]
77+
78+
79+
def test_enum_map_insert():
80+
class Bivalve(proto.Enum):
81+
CLAM = 0
82+
OYSTER = 1
83+
84+
class MolluscContainer(proto.Message):
85+
bivalves = proto.MapField(proto.STRING, proto.ENUM, number=1, enum=Bivalve,)
86+
87+
mc = MolluscContainer()
88+
clam = Bivalve.CLAM
89+
mc.bivalves["clam"] = clam
90+
mc.bivalves["oyster"] = 1
91+
92+
assert mc.bivalves == {"clam": clam, "oyster": Bivalve.OYSTER}

0 commit comments

Comments
 (0)