Check children for correct types and for any duplicates.
(children: Mapping[str, TreeNode])
| 195 | |
| 196 | @staticmethod |
| 197 | def _check_children(children: Mapping[str, TreeNode]) -> None: |
| 198 | """Check children for correct types and for any duplicates.""" |
| 199 | if not is_dict_like(children): |
| 200 | raise TypeError( |
| 201 | "children must be a dict-like mapping from names to node objects" |
| 202 | ) |
| 203 | |
| 204 | seen = set() |
| 205 | for name, child in children.items(): |
| 206 | if not isinstance(child, TreeNode): |
| 207 | raise TypeError( |
| 208 | f"Cannot add object {name}. It is of type {type(child)}, " |
| 209 | "but can only add children of type DataTree" |
| 210 | ) |
| 211 | |
| 212 | childid = id(child) |
| 213 | if childid not in seen: |
| 214 | seen.add(childid) |
| 215 | else: |
| 216 | raise InvalidTreeError( |
| 217 | f"Cannot add same node {name} multiple times as different children." |
| 218 | ) |
| 219 | |
| 220 | def __repr__(self) -> str: |
| 221 | return f"TreeNode(children={dict(self._children)})" |
no test coverage detected