InquirerPy icon indicating copy to clipboard operation
InquirerPy copied to clipboard

Fuzzy - "Exception list index out of range" on empty selection

Open averypierce opened this issue 2 years ago • 1 comments

Howdy! Got a bug report.

Python Version: 3.10 InquirerPy: 0.3.4

Steps to reproduce:

  • run code snippet below
  • on prompt, type "asdfasdf" or similar so that no results are shown for fuzzy match
  • Press Tab to trigger selection
  • Index error thrown
  • Press ENTER and prompt restarts nicely, but it is hard to hide the error text or inform user nicely as we are stuck in the prompt's loop

Code:

from InquirerPy import inquirer

result = inquirer.fuzzy(
    message="Selcet one:",
    choices=[1,2,3,4],
    multiselect=True,
).execute()

Output:

Unhandled exception in event loop:
  File "/depot/Python/Python-3.10/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/input/vt100.py", line 173, in callback_wrapper
    callback()
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/application/application.py", line 711, in read_from_input
    self.key_processor.process_keys()
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/key_processor.py", line 270, in process_keys
    self._process_coroutine.send(key_press)
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/key_processor.py", line 185, in _process
    self._call_handler(matches[-1], key_sequence=buffer[:])
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/key_processor.py", line 320, in _call_handler
    handler.call(event)
  File "/u/avery/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 124, in call
    result = self.handler(event)
  File "/depot/Python/Python-3.10/lib/python3.10/site-packages/InquirerPy/base/simple.py", line 240, in executable
    func(event)
  File "/depot/Python/Python-3.10/lib/python3.10/site-packages/InquirerPy/base/complex.py", line 132, in executable
    func(event)
  File "/depot/Python/Python-3.10/lib/python3.10/site-packages/InquirerPy/base/simple.py", line 142, in _
    method["func"](event, *method.get("args", []))
  File "/depot/Python/Python-3.10/lib/python3.10/site-packages/InquirerPy/prompts/fuzzy.py", line 629, in _handle_toggle_choice
    current_selected_index = self.content_control.selection["index"]
  File "/depot/Python/Python-3.10/lib/python3.10/site-packages/InquirerPy/prompts/fuzzy.py", line 251, in selection
    return self._filtered_choices[self.selected_choice_index]

Exception list index out of range
Press ENTER to continue...

I tried fiddling around with filters and validators to see if I could cause some valid index with a value I could reject, but it seems to error before that point.

As a short term workaround, error text can be hidden by wrapping with snippet below. User will see "Press ENTER to continue" with no stack trace.

with open(os.devnull, 'w') as devnull:
     with contextlib.redirect_stdout(devnull):

Been looking at the code a bit, I'll post a PR if I can

Thanks! Avery

averypierce avatar Nov 17 '22 18:11 averypierce

I've found this package only recently and I've stumbled into that same issue. I would love to see a patch in that regard.

anothertal3 avatar Oct 20 '23 13:10 anothertal3