mendeleev icon indicating copy to clipboard operation
mendeleev copied to clipboard

Remove hash support since it is unsafe for mutable objects

Open kalvdans opened this issue 4 years ago • 2 comments

Allowing hash on mutable objects leads to strange situations. For example:

>>> import mendeleev
>>> H = mendeleev.element(1)
>>> myset = {H}
>>> H.atomic_weight = 2
>>> H in myset
False
>>> H in list(myset)
True

This PR remove the hashing support so that instead we get an error:

>>> import mendeleev
>>> H = mendeleev.element(1)
>>> myset = {H}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'Element'

If you need to place elements or dict or set, you can use the .atomic_number property instead.

>>> import mendeleev
>>> H = mendeleev.element(1)
>>> myset = {H.atomic_number}
>>> H.atomic_number in myset
True
>>> H.atomic_number in list(myset)
True

kalvdans avatar Dec 21 '21 09:12 kalvdans

Can you point me to where hash is called on a mutable object in mendeleev?

lmmentel avatar Jan 29 '22 17:01 lmmentel

Can you point me to where hash is called on a mutable object in mendeleev?

Nowhere but other code using the library might start to put elements in a dict or set.

kalvdans avatar Jan 29 '22 19:01 kalvdans