Exemplo n.º 1
0
    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)

        self.remove_empty_stanzas(essence)
        return essence
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
 def purge(
     self,
     *,
     key: ids.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)
Exemplo n.º 4
0
 def purge(
     self,
     *,
     key: ids.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)
Exemplo n.º 5
0
def test_existing_key():
    d = {'abc': {'def': {'hij': 'val', 'hello': 'world'}}}
    remove(d, ['abc', 'def', 'hij'])
    assert d == {'abc': {'def': {'hello': 'world'}}}
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
def test_nonmapping_key():
    d = {'key': 'val'}
    with pytest.raises(TypeError):
        remove(d, ['key', 'sub'])
Exemplo n.º 8
0
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'}}
Exemplo n.º 9
0
def test_parent_cascaded_deletion_up_to_the_root():
    d = {'abc': {'def': {'hij': 'val'}}}
    remove(d, ['abc', 'def', 'hij'])
    assert d == {}
Exemplo n.º 10
0
def test_unexisting_key_in_unexisting_dict():
    d = {}
    remove(d, ['abc', 'def', 'hij'])
    assert d == {}
Exemplo n.º 11
0
def test_unexisting_key_in_existing_dict():
    d = {'abc': {'def': {'hello': 'world'}}}
    remove(d, ['abc', 'def', 'hij'])
    assert d == {'abc': {'def': {'hello': 'world'}}}