KeyError when Carousel contains children other than CarouselSlide #3812
Closed
Description
Description
I created a ui.carousel and used add_slot('control')
to add a 'q-carousel-control' element (as shown in the Quasar documentation examples). However, when switching slides, the following error occurs:
Traceback (most recent call last):
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/events.py", line 411, in handle_event
result = handler(arguments) if expects_arguments else handler()
^^^^^^^^^^^^^^^^^^
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/elements/mixins/value_element.py", line 40, in handle_change
self.set_value(self._event_args_to_value(e))
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/elements/mixins/value_element.py", line 108, in set_value
self.value = value
^^^^^^^^^^
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/binding.py", line 155, in __set__
self._change_handler(owner, value)
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/elements/mixins/value_element.py", line 23, in <lambda>
on_change=lambda sender, value: cast(Self, sender)._handle_value_change(value)) # pylint: disable=protected-access
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/elements/carousel.py", line 40, in _handle_value_change
names = [slide.props['name'] for slide in self]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/ruiheng/.cache/pypoetry/virtualenvs/lyceum-NKmSERnC-py3.11/lib/python3.11/site-packages/nicegui/elements/carousel.py", line 40, in <listcomp>
names = [slide.props['name'] for slide in self]
~~~~~~~~~~~^^^^^^^^
KeyError: 'name'
I believe this error occurs because the _handle_value_change
method of the Carousel class does not account for children other than CarouselSlide. The method assumes that all children have a 'name' property, which is not true for the 'q-carousel-control' element.