enthought/traits

[BUG] `Property` not notified of changes in `ReadOnly` trait.

Opened this issue · 0 comments

Using a Property depending on a nested trait in a ReadOnly, the Property doesn't fire change events, when the nested trait changed.
However, an observe on this nested trait fires.

from traits.api import Float, HasTraits, Instance, Property, ReadOnly


class Data(HasTraits):
    x = Float(default_value=0.0)


class DataManager(HasTraits):
    dataA = Instance(Data)
    dataB = ReadOnly()

    ax_square = Property(observe="dataA.x")
    bx_square = Property(observe="dataB.x")

    def _get_ax_square(self):
        return self.dataA.x**2

    def _get_bx_square(self):
        return self.dataB.x**2


def main():
    d = Data()

    dm = DataManager(dataA=d, dataB=d)
    dm.observe(lambda x: print(f"dataA.x: {x}"), "dataA.x")
    dm.observe(lambda x: print(f"dataB.x: {x}"), "dataB.x")

    dm.observe(lambda x: print(f"ax_square: {x}"), "ax_square")
    dm.observe(lambda x: print(f"bx_square: {x}"), "bx_square")

    d.x = 2.0


if __name__ == "__main__":
    main()

yields the output:

ax_square: TraitChangeEvent(object=<__main__.DataManager object at 0x7fa9f7e87680>, name='ax_square', old=<undefined>, new=4.0)
dataA.x: TraitChangeEvent(object=<__main__.Data object at 0x7fa9f7e87220>, name='x', old=0.0, new=2.0)
dataB.x: TraitChangeEvent(object=<__main__.Data object at 0x7fa9f7e87220>, name='x', old=0.0, new=2.0)