Skip to content

Commit

Permalink
fix: fix issue with equality comparison of repeated field with None (#…
Browse files Browse the repository at this point in the history
…477)

* fix: fix issue with equality comparison of repeated field with None

* style
  • Loading branch information
parthea authored Jul 29, 2024
1 parent e2f9c9d commit 3476348
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions proto/marshal/collections/repeated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import collections
import copy
from typing import Iterable

from proto.utils import cached_property

Expand Down Expand Up @@ -48,7 +49,7 @@ def __delitem__(self, key):
def __eq__(self, other):
if hasattr(other, "pb"):
return tuple(self.pb) == tuple(other.pb)
return tuple(self.pb) == tuple(other)
return tuple(self.pb) == tuple(other) if isinstance(other, Iterable) else False

def __getitem__(self, key):
"""Return the given item."""
Expand Down Expand Up @@ -119,7 +120,11 @@ def _pb_type(self):
def __eq__(self, other):
if super().__eq__(other):
return True
return tuple([i for i in self]) == tuple(other)
return (
tuple([i for i in self]) == tuple(other)
if isinstance(other, Iterable)
else False
)

def __getitem__(self, key):
return self._marshal.to_python(self._pb_type, self.pb[key])
Expand Down
1 change: 1 addition & 0 deletions tests/test_fields_repeated_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Baz(proto.Message):

baz = Baz(foos=[Foo(bar=42)])
assert baz.foos == baz.foos
assert baz.foos != None


def test_repeated_composite_init_struct():
Expand Down
1 change: 1 addition & 0 deletions tests/test_fields_repeated_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Foo(proto.Message):
foo = Foo(bar=[1, 1, 2, 3, 5, 8, 13])
assert foo.bar == copy.copy(foo.bar)
assert foo.bar != [1, 2, 4, 8, 16]
assert foo.bar != None


def test_repeated_scalar_del():
Expand Down

0 comments on commit 3476348

Please sign in to comment.