From 1a500bfa2477bbb305dec27eed880d66ddff7d2c Mon Sep 17 00:00:00 2001 From: Max Dallabetta Date: Thu, 18 Apr 2024 14:15:04 +0200 Subject: [PATCH] fix a bug in bf_search regarding boolean values --- src/fundus/parser/data.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/fundus/parser/data.py b/src/fundus/parser/data.py index 3a6013c25..166311eec 100644 --- a/src/fundus/parser/data.py +++ b/src/fundus/parser/data.py @@ -18,6 +18,8 @@ LDMappingValue: TypeAlias = Union[List[Dict[str, Any]], Dict[str, Any]] +_sentinel = object() + class LinkedDataMapping: """ @@ -123,19 +125,28 @@ def bf_search(self, key: str, depth: Optional[int] = None, default: Any = None) def search_recursive(nodes: Iterable[LDMappingValue], current_depth: int): if current_depth == depth: - return None + return _sentinel else: new: List[Dict[str, Any]] = [] for node in nodes: if isinstance(node, list): new.extend(node) continue - elif value := node.get(key): + elif (value := node.get(key, _sentinel)) is not _sentinel: return value new.extend(v for v in node.values() if isinstance(v, dict)) - return search_recursive(new, current_depth + 1) if new else None - return search_recursive([self.__dict__], 0) or default + if not new: + return _sentinel + + return search_recursive(new, current_depth + 1) + + result = search_recursive([self.__dict__], 0) + + if result == _sentinel: + return default + + return result def __repr__(self): return f"LD containing '{', '.join(content)}'" if (content := self.__dict__.keys()) else "Empty LD"