diff --git a/pyrsistent/_transformations.py b/pyrsistent/_transformations.py index 7544843..6ef747f 100644 --- a/pyrsistent/_transformations.py +++ b/pyrsistent/_transformations.py @@ -127,6 +127,10 @@ def _update_structure(structure, kvs, path, command): for k, v in kvs: is_empty = False if v is _EMPTY_SENTINEL: + if command is discard: + # If nothing there when discarding just move on, do not introduce new nodes + continue + # Allow expansion of structure but make sure to cover the case # when an empty pmap is added as leaf node. See #154. is_empty = True diff --git a/tests/transform_test.py b/tests/transform_test.py index 770fb47..d133d14 100644 --- a/tests/transform_test.py +++ b/tests/transform_test.py @@ -115,3 +115,8 @@ def test_discard_multiple_elements_in_pvector(): def test_transform_insert_empty_pmap(): m = pmap().transform(['123'], pmap()) assert m == pmap({'123': pmap()}) + + +def test_discard_does_not_insert_nodes(): + m = freeze({}).transform(['foo', 'bar'], discard) + assert m == pmap({})