Skip to content

Commit

Permalink
Fix invalid UniqueConstraint error message when loading a TypedTree
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Nov 3, 2023
1 parent 87699e1 commit 13dc022
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.6.1 (unreleased)
- Add optional `tree.print(..., file=IO)` argument.
- Fix invalid UniqueConstraint error message when loading a TypedTree.

## 0.6.0 (2023-11-01)

Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ node_id (int, readonly):
It cannot be changed later.

kind (str, readonly):
Used by :class:`~nutree.typed_tree.TypedNode` (see `Typed child nodes <ug_graphs.html>`_).
Used by :class:`~nutree.typed_tree.TypedNode` (see :ref:`Typed child nodes <typed-tree>`).

.. General API
.. -----------
Expand Down
10 changes: 7 additions & 3 deletions nutree/typed_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def add_child(
raise ValueError("Cannot set ID for deep copies.")
source_node = child
if source_node._tree is self._tree:
if source_node._parent is self._parent:
if source_node._parent is self:
raise UniqueConstraintError(
f"Same parent not allowed: {source_node}"
)
Expand Down Expand Up @@ -665,12 +665,16 @@ def iter_by_type(self, kind: Union[str, ANY_KIND]) -> Iterator[TypedNode]:
@classmethod
def _from_list(cls, obj: List[Dict], *, mapper=None) -> TypedTree:
tree = cls()

# System root has index #0:
node_idx_map = {0: tree._root}

# Start reading data lines starting at index #1:
for idx, (parent_idx, data) in enumerate(obj, 1):
parent = node_idx_map[parent_idx]
# print(idx, parent_idx, data, parent)
if type(data) is str:
# this can only happen if the source was generate by a plain Tree
# This can only happen if the source was generated by a plain Tree
n = parent.add(data, kind=DEFAULT_CHILD_TYPE)
elif type(data) is int:
first_clone = node_idx_map[data]
Expand All @@ -683,7 +687,7 @@ def _from_list(cls, obj: List[Dict], *, mapper=None) -> TypedTree:
data_obj = call_mapper(mapper, parent, data)
n = parent.add(data_obj, kind=kind, data_id=data_id)
elif isinstance(data, dict) and "str" in data:
# this can happen if the source was generate by without a
# This can happen if the source was generated without a
# serialization mapper, for a TypedTree that has str nodes
n = parent.add(data["str"], kind=data.get("kind"))
else:
Expand Down

0 comments on commit 13dc022

Please sign in to comment.