Skip to content

Commit

Permalink
Fix error with __getattr__ triggering recursion limit error
Browse files Browse the repository at this point in the history
  • Loading branch information
pnorton-usgs committed Jan 2, 2025
1 parent f81b4d6 commit f14eba0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyPRMS/dimensions/Dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def __getattr__(self, name: str) -> Any:
"""

# print('ATTR: {}'.format(name))
# https://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
if name == "__setstate__":
raise AttributeError(name)
return getattr(self.__dimensions, name)

def __getitem__(self, item: str) -> Dimension:
Expand Down
3 changes: 3 additions & 0 deletions pyPRMS/parameters/Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def __getattr__(self, name: str):

# Undefined attributes will look up the given parameter
# return self.get(item)
# https://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
if name == "__setstate__":
raise AttributeError(name)
return getattr(self.__parameters, name)

def __getitem__(self, item):
Expand Down
12 changes: 12 additions & 0 deletions tests/func/test_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ def test_get_missing_dimension(self, dims_obj):

with pytest.raises(ValueError):
dims_obj.get('somedim')

def test_dunder_getitem(self, dims_obj):
"""Test the __getitem__ method"""

with pytest.raises(KeyError):
dims_obj['somedim']

def test_dunder_getattr(self, dims_obj):
"""Test the __getattr__ method"""

with pytest.raises(AttributeError):
dims_obj.somedim

0 comments on commit f14eba0

Please sign in to comment.