Skip to content

Commit

Permalink
Fix #268 2, do not rely on well implemented __ne__ for keys in pmaps,…
Browse files Browse the repository at this point in the history
… instead do explicit inversion of equality comparison when checking for inequality
  • Loading branch information
tobgu committed Oct 22, 2023
1 parent 0be88dd commit 3fa47c2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pyrsistent/_pmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ def set(self, key, val):
for k, v in bucket:
if k == key:
if v is not val:
new_bucket = [(k2, v2) if k2 != k else (k2, val) for k2, v2 in bucket]
# Use `not (k2 == k)` rather than `!=` to avoid relying on a well implemented `__ne__`, see #268.
new_bucket = [(k2, v2) if not (k2 == k) else (k2, val) for k2, v2 in bucket]
self._buckets_evolver[index] = new_bucket

return self
Expand Down Expand Up @@ -476,7 +477,8 @@ def remove(self, key):
index, bucket = PMap._get_bucket(self._buckets_evolver, key)

if bucket:
new_bucket = [(k, v) for (k, v) in bucket if k != key]
# Use `not (k == key)` rather than `!=` to avoid relying on a well implemented `__ne__`, see #268.
new_bucket = [(k, v) for (k, v) in bucket if not (k == key)]
size_diff = len(bucket) - len(new_bucket)
if size_diff > 0:
self._buckets_evolver[index] = new_bucket if new_bucket else None
Expand Down
4 changes: 2 additions & 2 deletions tests/map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,5 +547,5 @@ def test_pmap_removal_with_broken_classes_deriving_from_namedtuple():

# Both items are removed due to how they are compared for inequality
assert BrokenPerson('X') not in s
assert BrokenItem('X') not in s
assert len(s) == 0
assert BrokenItem('X') in s
assert len(s) == 1

0 comments on commit 3fa47c2

Please sign in to comment.