'Signals and inheritance: pattern to avoid parent triggering signals too early

I have a model class that emits a signal when changed:

class Foo(QObject):

  has_changed = pyqtSignal()
  
  def update_foo(self, ...):
    ...
    self.has_changed.emit()

Now I want to extend this model to include more features.

class FooDeluxe(Foo):

  def update_foo(self, ...):
    super().update_foo()  # emits has_changed even though FooDeluxe not ready
    ...
    self.has_changed.emit()

The problem is that when I call the parent's update_foo, it triggers has_changed even though FooDeluxe is not completely updated yet.

I am pretty sure this is a rather common situation and I was wondering what is the best practice pattern to overcome it. Having a different signal for FooDeluxe beats the purpose of inheritance and wouldn't scale up very well. We could also make sure signals are always emitted in a separate function like

class Foo(QObject):
  ...
  def _update_foo(self, ...):
    ...
    return has_changed
  def update_foo(self, ...):
    if self._update_foo(...)
      self.has_changed.emit()

class FooDeluxe(Foo):
  ...
  def _update_foo(self, ...):
    super()._update_foo(...)
    ...
    return has_changed
  def update_foo(self, ...):
    if self._update_foo(...):
      self.has_changed.emit()

I think this would work, and if we have total control of Foo this is probably not to bad of a solution, but it supposed a particular implemention for the base model class, of which we may not have control.

What are the standard patterns for this problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source