Closed mwcraig closed 1 year ago
hi @mwcraig - thanks for reporting this -
i think you're getting changes + 1
events because the change in _value
is also registering a change:
https://github.com/maxfordham/ipyautoui/blob/6a95087a3e08a451fae3900e1ca3c94ba0a7763c/src/ipyautoui/autoipywidget.py#L651-L659
Agree that you should only get 1no change reportied, and agree that with self.hold_trait_notifications()
is probs the best way to go. I'll take a look at this when I get a chance.
I was using
AutoUi
to generate some forms when I encountered unexpected behavior when I observe the_value
of a widget: my observer is called multiple times. The number of times is the number of values in the widget that are changing plus 1.In my particular case, the intermediate calls were causing issues with validation of my model.
To make this more concrete, here is an example that reproduces the problem:
Setup
Consider this model (there are design problems here, but please ignore those):
Next, create a ui and an observer:
Finally, set the value of the ui:
Expected outcome
My observer will be called once.
Actual outcome
My observer is called 4 times:
The number of calls is the number of values being changed in the UI widget plus one, I think because of this line.
Partial fix
This can be partly addressed by wrapping the body of
ipyautoui.autoipywidget.AutoObject._update_widgets_from_value
with a context managerwith self.hold_trait_notifications()
.When I try that I still end up with my observer being called twice instead of once, but in both calls the value passes a validation check, which fails when I don't make this change.