8000 fix: fix issue with equality comparison of repeated field with None (… · googleapis/proto-plus-python@3476348 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3476348

Browse files
authored
fix: fix issue with equality comparison of repeated field with None (#477)
* fix: fix issue with equality comparison of repeated field with None * style
1 parent e2f9c9d commit 3476348

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

proto/marshal/collections/repeated.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import collections
1616
import copy
17+
from typing import Iterable
1718

1819
from proto.utils import cached_property
1920

@@ -48,7 +49,7 @@ def __delitem__(self, key):
4849
def __eq__(self, other):
4950
if hasattr(other, "pb"):
5051
return tuple(self.pb) == tuple(other.pb)
51-
return tuple(self.pb) == tuple(other)
52+
return tuple(self.pb) == tuple(other) if isinstance(other, Iterable) else False
5253

5354
def __getitem__(self, key):
5455
"""Return the given item."""
@@ -119,7 +120,11 @@ def _pb_type(self):
119120
def __eq__(self, other):
120121
if super().__eq__(other):
121122
return True
122-
return tuple([i for i in self]) == tuple(other)
123+
return (
124+
tuple([i for i in self]) == tuple(other)
125+
if isinstance(other, Iterable)
126+
else False
127+
)
123128

124129
def __getitem__(self, key):
125130
return self._marshal.to_python(self._pb_type, self.pb[key])

tests/test_fields_repeated_composite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Baz(proto.Message):
4747

4848
baz = Baz(foos=[Foo(bar=42)])
4949
assert baz.foos == baz.foos
50+
assert baz.foos != None
5051

5152

5253
def test_repeated_composite_init_struct():

tests/test_fields_repeated_scalar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Foo(proto.Message):
7272
foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13])
7373
assert foo.bar == copy.copy(foo.bar)
7474
assert foo.bar != [1, 2, 4, 8, 16]
75+
assert foo.bar != None
7576

7677

7778
def test_repeated_scalar_del():

0 commit comments

Comments
 (0)
0