File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -227,9 +227,18 @@ def test_overwrite_child(self):
227
227
assert marys_evil_twin .parent is john
228
228
229
229
230
- # TODO write and test all the del methods
231
230
class TestPruning :
232
- ...
231
+ def test_del_child (self ):
232
+ john = TreeNode ()
233
+ mary = TreeNode ()
234
+ john ._set_item ("Mary" , mary )
235
+
236
+ del john ["Mary" ]
237
+ assert "Mary" not in john .children
238
+ assert mary .parent is None
239
+
240
+ with pytest .raises (KeyError ):
241
+ del john ["Mary" ]
233
242
234
243
235
244
def create_test_tree ():
Original file line number Diff line number Diff line change @@ -438,8 +438,14 @@ def _set_item(
438
438
else :
439
439
current_node ._set (name , item )
440
440
441
- def del_node (self , path : str ):
442
- raise NotImplementedError
441
+ def __delitem__ (self : Tree , key : str ):
442
+ """Remove a child node from this tree object."""
443
+ if key in self .children :
444
+ child = self ._children [key ]
445
+ del self ._children [key ]
446
+ child .orphan ()
447
+ else :
448
+ raise KeyError ("Cannot delete" )
443
449
444
450
def update (self : Tree , other : Mapping [str , Tree ]) -> None :
445
451
"""
Original file line number Diff line number Diff line change @@ -36,6 +36,8 @@ New Features
36
36
By `Tom Nicholas <https://github.com/TomNicholas >`_.
37
37
- New HTML repr, which will automatically display in a jupyter notebook. (:pull: `78 `)
38
38
By `Tom Nicholas <https://github.com/TomNicholas >`_.
39
+ - New delitem method so you can delete nodes. (:pull: `88 `)
40
+ By `Tom Nicholas <https://github.com/TomNicholas >`_.
39
41
40
42
Breaking changes
41
43
~~~~~~~~~~~~~~~~
You can’t perform that action at this time.
0 commit comments