From ccbdb144678abf9f2a828d40eb8dbc404e2fb544 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Fri, 10 Nov 2023 22:35:26 +0000 Subject: [PATCH 1/3] fix #214: AttributeDict does not support hasattr --- gromacs/utilities.py | 11 ++++++++++- tests/test_utilities.py | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gromacs/utilities.py b/gromacs/utilities.py index 097760a9..e29dd96c 100644 --- a/gromacs/utilities.py +++ b/gromacs/utilities.py @@ -120,10 +120,19 @@ class AttributeDict(dict): """A dictionary with pythonic access to keys as attributes --- useful for interactive work.""" def __getattribute__(self, x): + # First try to get the attribute from the class parent (AttributeDict) try: return super(AttributeDict, self).__getattribute__(x) + + # AttributeError is raised if the attribute doesn't exist except AttributeError: - return self[x] + # Next try to get the attribute from self (dict) + try: + return self[x] + + # If that fails, raise an AttributeError exception + except KeyError: + raise AttributeError def __setattr__(self, name, value): try: diff --git a/tests/test_utilities.py b/tests/test_utilities.py index d8707bef..cba87bc5 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -52,6 +52,11 @@ def test_dict_set(self): self.d["gargl"] = "blaster" assert self.d["gargl"] == "blaster" + def test_dict_hasttr(self): + assert hasattr(self.d, "foo") + assert hasattr(self.d, "baz") + assert not hasattr(self.d, "bar") + def test_pickle(self): try: dump = pickle.dumps(self.d, pickle.HIGHEST_PROTOCOL) From 4814f67e53eaae4d14658816194699154aeee5c8 Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Sat, 11 Nov 2023 08:50:27 +0000 Subject: [PATCH 2/3] review: comments from Oliver --- tests/test_utilities.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index cba87bc5..e4048c5f 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -52,7 +52,7 @@ def test_dict_set(self): self.d["gargl"] = "blaster" assert self.d["gargl"] == "blaster" - def test_dict_hasttr(self): + def test_dict_hasattr(self): assert hasattr(self.d, "foo") assert hasattr(self.d, "baz") assert not hasattr(self.d, "bar") From cff497972175ddf3b4b974d1b1cb0c91d313ed1d Mon Sep 17 00:00:00 2001 From: Jan Domanski Date: Sat, 11 Nov 2023 08:51:56 +0000 Subject: [PATCH 3/3] update CHANGES --- CHANGES | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES b/CHANGES index abb880dd..76dcc98a 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,7 @@ orbeckst, jandom * removed support for legacy Python (<= 3.7) (#259) * fixed GROMACS TOP reader not reading angle parameters from topology file (#261) +* fixed AttributeDict does not support hasattr (#214) 2023-09-16 0.8.5