From 515ee9da89750bed8602f11e68ee7ea457acc933 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Sun, 28 Jun 2026 15:39:55 +0200 Subject: [PATCH] fix: to_dict() returns None for non-expanded nodes When a node has expanded=False, to_dict() skips the return statement entirely, returning None instead of the expected tag string or dict with data. This also breaks to_json() which returns 'null' for collapsed trees. Add else clause to handle the non-expanded case consistently with the expanded-but-empty-children case. --- treelib/tree.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/treelib/tree.py b/treelib/tree.py index c2594f3..2635a8e 100644 --- a/treelib/tree.py +++ b/treelib/tree.py @@ -1976,7 +1976,9 @@ def to_dict( ) if len(tree_dict[ntag]["children"]) == 0: tree_dict = self[nid].tag if not with_data else {ntag: {"data": self[nid].data}} - return tree_dict + else: + tree_dict = self[nid].tag if not with_data else {ntag: {"data": self[nid].data}} + return tree_dict def to_json(self, with_data: bool = False, sort: bool = True, reverse: bool = False): """