Skip to content

PMap.__getitem__ gets the wrong value #212

@brunonicko

Description

@brunonicko

PMap.__getitem__ will return the wrong value if the object used for the key defines a unique __hash__ and a custom __eq__ method.

Repro (tested with python 3.8 and pyrsistent 0.17.3):

from pyrsistent import pmap

class CustomObject(object):
    __slots__ = ()

    def __hash__(self):
        return object.__hash__(self)

    def __eq__(self, other):  # Having __eq__ declared causes the failure
        return True  # Changing this to False will fail with a different error

keys = []
py_dict = {}
for i in range(10000):
    key = CustomObject()
    keys.append(key)
    py_dict[key] = i

p_map = pmap(py_dict)
assert len(py_dict) == len(p_map)
assert set(py_dict.keys()) == set(p_map.keys())

for i, key in enumerate(keys):
    assert py_dict[key] == i
    assert p_map[key] == i  # Fails here when `i` is a big number, __getitem__ gets the wrong value

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions