Skip to content

Commit b01d8d1

Browse files
committed
Fix recursion issue in DictToObject.__getattr__ and improve undefined class handling
- Fixed infinite recursion when accessing _undefined_class attribute - Use __dict__ direct access to avoid __getattr__ loops - Improved convert_dict_to_object to properly set undefined_class - All tests now pass - missing fields handled gracefully without errors This completes the fix for the missing fields handling issue.
1 parent 5ba6e84 commit b01d8d1

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ def __getitem__(self, key):
7575

7676
def __getattr__(self, name):
7777
"""Handle missing attributes gracefully"""
78-
# Return the undefined class instance that was set globally
79-
# This allows the undefined behavior to be handled properly
80-
undefined_class = getattr(self, '_undefined_class', SilentUndefined)
78+
# Avoid recursion by checking __dict__ directly
79+
if name == '_undefined_class':
80+
return SilentUndefined # Default fallback
81+
82+
# Return the undefined class instance that was set
83+
undefined_class = self.__dict__.get('_undefined_class', SilentUndefined)
8184
return undefined_class(name=name)
8285

8386
def __contains__(self, key):
@@ -93,11 +96,13 @@ def __iter__(self):
9396
return iter(self._original_dict.keys())
9497

9598

96-
def convert_dict_to_object(data, undefined_class=SilentUndefined):
99+
def convert_dict_to_object(data, undefined_class=None):
97100
"""Recursively convert dictionaries to objects for dot notation access"""
98101
if isinstance(data, dict):
99102
obj = DictToObject(data)
100-
obj._undefined_class = undefined_class
103+
# Set the undefined class in __dict__ to avoid __getattr__ recursion
104+
if undefined_class is not None:
105+
obj.__dict__['_undefined_class'] = undefined_class
101106
return obj
102107
elif isinstance(data, list):
103108
return [convert_dict_to_object(item, undefined_class) for item in data]

0 commit comments

Comments
 (0)