You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current implementation of ChoiceElement.set_options() calls self.update() twice if a value is set, causing bugs in code that relies on data coming from on_changed() to be accurate.
#3733
Closed
sSasha-uwu opened this issue
Sep 15, 2024
· 2 comments
· Fixed by #3736
Just ran into this issue while working with the ui.radio() element. When I call set_options() on the radio element with a value set, the on_changed() callback is triggered twice due to the duplicated self.update() calls within the set_options() function. Not only is it strange to call update() twice for no reason, it causes bugs. If the current value of the radio is not within the new set of options, it will call on_changed() once with a None, and then a second time with the correct value. This causes seemingly impossible NoneType errors downstream that now need to be accounted for.
Fortunately, this is incredibly simple to fix.
Original code from nicegui/elements/choice_element.py > ChoiceElement() > set_options():
defset_options(self, options: Union[List, Dict], *, value: Any= ...) ->None:
"""Set the options of this choice element. :param options: The new options. :param value: The new value. If not given, the current value is kept. """self.options=optionsself.update()
ifvalueisnot ...:
self.value=valueself.update()
Fixed code:
defset_options(self, options: Union[List, Dict], *, value: Any= ...) ->None:
"""Set the options of this choice element. :param options: The new options. :param value: The new value. If not given, the current value is kept. """self.options=optionsifvalueisnot ...:
self.value=valueself.update()
If someone specifically wanted this functionality for whatever reason, they could call set_options() and set_value() separately, achieving the duplicated call regardless, so nothing is lost by making this change.
The text was updated successfully, but these errors were encountered:
Looks good to me. Would you like to create a pull request @sSasha-uwu? We would be happy to have you as a contributor. Ideally you would also add a test to verify correct behaviour.
Interesting. I guess I added the if-block later and didn't think about self.update() doing more than just updating the frontend. And since consecutive updates get eliminated when enqueued in the outbox, it didn't seem like a problem.
Anyway, the solution looks totally reasonable to me. 👍
Description
Just ran into this issue while working with the
ui.radio()
element. When I call set_options() on the radio element with a value set, theon_changed()
callback is triggered twice due to the duplicatedself.update()
calls within theset_options()
function. Not only is it strange to call update() twice for no reason, it causes bugs. If the current value of the radio is not within the new set of options, it will callon_changed()
once with aNone
, and then a second time with the correct value. This causes seemingly impossible NoneType errors downstream that now need to be accounted for.Fortunately, this is incredibly simple to fix.
Original code from
nicegui/elements/choice_element.py > ChoiceElement() > set_options()
:Fixed code:
If someone specifically wanted this functionality for whatever reason, they could call
set_options()
andset_value()
separately, achieving the duplicated call regardless, so nothing is lost by making this change.The text was updated successfully, but these errors were encountered: