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 equality check when other contains more keys #124

Merged
merged 3 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def __eq__(self, other):
if i1 != i2 or v1 != v2:
return False
return True
if len(self) != len(other):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must be possible to move this to before line 114 and remove check at line 117. They are equal, so no need to have it copy-pasted in two places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree.
Check from line 117 is faster than line 123: no python magic method __len__ is involved.
Code duplication is very low, let's merge PR as is.

Copy link
Member

@webknjaz webknjaz Aug 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's about speed, then I'd convert len(self) into len(self._impl._items)

return False
for k, v in self.items():
nv = other.get(k, _marker)
if v != nv:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def test_eq3(self):
d2 = self.make_dict()
self.assertNotEqual(d1, d2)

def test_eq_other_mapping_contains_more_keys(self):
d1 = self.make_dict(foo='bar')
d2 = dict(foo='bar', bar='baz')
self.assertNotEqual(d1, d2)

def test_ne(self):
d = self.make_dict([('key', 'value1')])
self.assertNotEqual(d, {'key': 'another_value'})
Expand Down