Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issue with equality comparison of repeated field with None #477

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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