Skip to content

Commit e7fe232

Browse files
authored
Fix infinite recursion (#122)
* don't do the parent dict * Update CHANGELOG.rst * use name or repr for parent
1 parent ac2311b commit e7fe232

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**4.1.2 - 12/3/24**
2+
3+
- Fix infinite recursion bug
4+
15
**4.1.1 - 12/2/24**
26

37
- Properly clean up Jenkins workspaces

src/gbd_mapping/base_template.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
class GbdRecord:
1111
"""Base class for entities modeled in the GBD."""
1212
__slots__ = ()
13-
13+
1414
def to_dict(self):
1515
out = {}
1616
for item in self.__slots__:
1717
attr = getattr(self, item)
18-
if isinstance(attr, GbdRecord):
18+
if item == "parent":
19+
out[item] = attr.name if hasattr(attr, "name") else attr.__repr__()
20+
elif isinstance(attr, GbdRecord):
1921
out[item] = attr.to_dict()
2022
elif isinstance(attr, tuple) and attr:
2123
if isinstance(attr[0], GbdRecord):
@@ -38,7 +40,7 @@ def __getitem__(self, item):
3840
def __iter__(self):
3941
for item in self.__slots__:
4042
yield getattr(self, item)
41-
43+
4244
def __eq__(self, other):
4345
return all([getattr(self, item) == getattr(other, item) for item in self.__slots__
4446
if not isinstance(getattr(self, item), GbdRecord)])

tests/test_base_template.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from gbd_mapping.base_template import GbdRecord
2+
3+
4+
class TestGbdRecord(GbdRecord):
5+
__slots__ = ("name", "parent")
6+
7+
def __init__(self, name, parent=None):
8+
super().__init__()
9+
self.name = name
10+
self.parent = parent
11+
12+
13+
def test_to_dict():
14+
record = TestGbdRecord(name="record1")
15+
record2 = TestGbdRecord(name="record2", parent=record)
16+
record.parent = record2
17+
record.to_dict()

0 commit comments

Comments
 (0)