def clear(self, *, essence: bodies.BodyEssence) -> bodies.BodyEssence: essence = super().clear(essence=essence) # Work around an issue with mypy not treating TypedDicts as MutableMappings. essence_dict = cast(Dict[Any, Any], essence) dicts.remove(essence_dict, self.field) return essence
def build( self, *, body: bodies.Body, extra_fields: Optional[Iterable[dicts.FieldSpec]] = None, ) -> bodies.BodyEssence: essence = super().build(body=body, extra_fields=extra_fields) # Work around an issue with mypy not treating TypedDicts as MutableMappings. essence_dict = cast(Dict[Any, Any], essence) dicts.remove(essence_dict, self.field) return essence
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() key_field = self.field + (key, ) body_value = dicts.resolve(body, key_field, absent) patch_value = dicts.resolve(patch, key_field, absent) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() for full_key in self.make_keys(key, body=body): key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, absent) patch_value = dicts.resolve(patch, key_field, absent) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() full_key = self.make_key(key) key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, absent, assume_empty=True) patch_value = dicts.resolve(patch, key_field, absent, assume_empty=True) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def test_existing_key(): d = {'abc': {'def': {'hij': 'val', 'hello': 'world'}}} remove(d, ['abc', 'def', 'hij']) assert d == {'abc': {'def': {'hello': 'world'}}}
def test_empty_path(): d = {} with pytest.raises(ValueError) as e: remove(d, []) assert "Removing a root of a dict is impossible" in str(e.value)
def test_nonmapping_key(): d = {'key': 'val'} with pytest.raises(TypeError): remove(d, ['key', 'sub'])
def test_parent_cascaded_deletion_up_to_a_middle(): d = {'abc': {'def': {'hij': 'val'}, 'hello': 'world'}} remove(d, ['abc', 'def', 'hij']) assert d == {'abc': {'hello': 'world'}}
def test_parent_cascaded_deletion_up_to_the_root(): d = {'abc': {'def': {'hij': 'val'}}} remove(d, ['abc', 'def', 'hij']) assert d == {}
def test_unexisting_key_in_unexisting_dict(): d = {} remove(d, ['abc', 'def', 'hij']) assert d == {}
def test_unexisting_key_in_existing_dict(): d = {'abc': {'def': {'hello': 'world'}}} remove(d, ['abc', 'def', 'hij']) assert d == {'abc': {'def': {'hello': 'world'}}}