From 5c5f09f15f0967c7b09a4f46d282b64f22dd9a29 Mon Sep 17 00:00:00 2001 From: DNIIBOY Date: Sun, 25 Feb 2024 17:49:23 +0100 Subject: [PATCH] Update __hash__ to use all of self.attrs --- chempy/chemistry.py | 17 ++++++++--------- chempy/tests/test_chemistry.py | 1 + 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/chempy/chemistry.py b/chempy/chemistry.py index 5e45ea3a..e225dc7f 100644 --- a/chempy/chemistry.py +++ b/chempy/chemistry.py @@ -99,15 +99,14 @@ def __eq__(self, other): return True def __hash__(self) -> int: - return sum( - map( - hash, - ( - getattr(self, k) - for k in ("name", "latex_name", "unicode_name", "html_name") - ), - ) - ) + hashed_values = [] + for key in self.attrs: + value = getattr(self, key) + if isinstance(value, dict): + hashed_values.append(hash(tuple(sorted(value.items())))) + else: + hashed_values.append(hash(value)) + return sum(hashed_values) @property def charge(self): diff --git a/chempy/tests/test_chemistry.py b/chempy/tests/test_chemistry.py index ec823837..2307be57 100644 --- a/chempy/tests/test_chemistry.py +++ b/chempy/tests/test_chemistry.py @@ -41,6 +41,7 @@ def test_Substance(): assert s.charge == 1 assert abs(s.mass - 1.008) < 1e-3 assert s in {s: 1} + assert hash(s) != hash(Substance.from_formula("He")) def test_Substance__2():