From 13a3d834de31e9c2a2d9e58ea0c92ea056deec56 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 17 Jan 2024 08:37:14 +0100 Subject: [PATCH] Updated the getattr patch (#699) # Description Ensure that we can access annotations added by imported ontologies. This issue was revealed when running the periodictable script for EMMO 1.0.0-beta5. ## Type of change - [x] Bug fix. - [ ] New feature. - [ ] Documentation update. - [ ] Test update. ## Checklist This checklist can be used as a help for the reviewer. - [ ] Is the code easy to read and understand? - [ ] Are comments for humans to read, not computers to disregard? - [ ] Does a new feature has an accompanying new test (in the CI or unit testing schemes)? - [ ] Has the documentation been updated as necessary? - [ ] Does this close the issue? - [ ] Is the change limited to the issue? - [ ] Are errors handled for all outcomes? - [ ] Does the new feature provide new restrictions on dependencies, and if so is this documented? ## Comments Co-authored-by: Francesca L. Bleken <48128015+francescalb@users.noreply.github.com> --- ontopy/patch.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ontopy/patch.py b/ontopy/patch.py index 61c68e50c..2b744b5aa 100644 --- a/ontopy/patch.py +++ b/ontopy/patch.py @@ -29,6 +29,7 @@ def render_func(entity): # Extending ThingClass (classes) # ============================== +# Save a copy of the unpatched ThingClass.__getattr__() method. save_getattr = ThingClass.__getattr__ @@ -133,7 +134,13 @@ def _getattr(self, name): entity = self.namespace.ontology.get_by_label(name) # add annotation property to world._props for faster access later self.namespace.world._props[name] = entity - return save_getattr(self, entity.name) + + # Try first unpatched getattr method to avoid risking + # infinite recursion. + try: + return save_getattr(self, entity.name) + except AttributeError: + return getattr(self, entity.name) raise err