handle deletions correctly

This commit is contained in:
Jakob Ketterl 2021-03-06 23:48:31 +01:00
parent e0985c3802
commit 161408dbf4
2 changed files with 16 additions and 3 deletions

View File

@ -322,11 +322,13 @@ class PropertyStack(PropertyManager):
def receiveEvent(self, layer, changes): def receiveEvent(self, layer, changes):
changesToForward = {name: value for name, value in changes.items() if layer == self._getTopLayer(name)} changesToForward = {name: value for name, value in changes.items() if layer == self._getTopLayer(name)}
# deletions need to be handled separately: only send them if deleted in all layers # deletions need to be handled separately:
# * send a deletion if the key was deleted in all layers
# * send lower value if the key is still present in a lower layer
deletionsToForward = { deletionsToForward = {
name: value name: PropertyDeleted if self._getTopLayer(name, False) is None else self[name]
for name, value in changes.items() for name, value in changes.items()
if value is PropertyDeleted and self._getTopLayer(name, False) is None if value is PropertyDeleted
} }
self._fireCallbacks({**changesToForward, **deletionsToForward}) self._fireCallbacks({**changesToForward, **deletionsToForward})

View File

@ -215,3 +215,14 @@ class PropertyStackTest(TestCase):
ps.wire(mock.method) ps.wire(mock.method)
del low_pm["testkey"] del low_pm["testkey"]
mock.method.assert_called_once_with({"testkey": PropertyDeleted}) mock.method.assert_called_once_with({"testkey": PropertyDeleted})
def testChangeEventWhenKeyDeleted(self):
ps = PropertyStack()
low_pm = PropertyLayer(testkey="lowvalue")
high_pm = PropertyLayer(testkey="highvalue")
ps.addLayer(0, high_pm)
ps.addLayer(1, low_pm)
mock = Mock()
ps.wire(mock.method)
del high_pm["testkey"]
mock.method.assert_called_once_with({"testkey": "lowvalue"})