CITGuru / PyInquirer

A Python module for common interactive command line user interfaces
MIT License
1.92k stars 236 forks source link

Setting default causes exception on rawlists #18

Open martenjacobs opened 5 years ago

martenjacobs commented 5 years ago

Using the default-key causes an exception when used on a rawlist

Consider the following code:

from PyInquirer import prompt, print_json

answer = prompt([{
        'type': 'rawlist',
        'name': "test-question",
        'message': "Select item",
        'default': 2,
        'choices': [
            {   "name"  : "Item 1",
                "value" : "item1"   },
            {   "name"  : "Item 2",
                "value" : "item2"   },
            {   "name"  : "Item 3",
                "value" : "item3"   },
            {   "name"  : "Item 4",
                "value" : "item4"   },
        ]
    }])

print_json(answer)

Expected result:

Item 3 is initially selected in the prompt

Actual result:

? Select item
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/cache.py", line 34, in get
    return self._data[key]
KeyError: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./scripts/test.py", line 20, in <module>
    question
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyInquirer/prompt.py", line 71, in prompt
    eventloop=eventloop)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/shortcuts.py", line 625, in run_application
    result = cli.run()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/interface.py", line 413, in run
    self._redraw()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/interface.py", line 358, in _redraw
    self.renderer.render(self, self.layout, is_done=self.is_done)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/renderer.py", line 429, in render
    extended_height=size.rows,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 142, in write_to_screen
    sizes = self._divide_heigths(cli, write_position)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 177, in _divide_heigths
    dimensions = [get_dimension_for_child(c, index) for index, c in enumerate(self.children)]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 177, in <listcomp>
    dimensions = [get_dimension_for_child(c, index) for index, c in enumerate(self.children)]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 175, in get_dimension_for_child
    return c.preferred_height(cli, write_position.width, write_position.extended_height)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 1652, in preferred_height
    return self.content.preferred_height(cli, width, max_available_height)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/containers.py", line 1000, in preferred_height
    cli, width - total_margin_width, max_available_height, wrap_lines),
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/controls.py", line 254, in preferred_height
    content = self.create_content(cli, width, None)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/controls.py", line 259, in create_content
    tokens_with_mouse_handlers = self._get_tokens_cached(cli)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/controls.py", line 239, in _get_tokens_cached
    cli.render_counter, lambda: self.get_tokens(cli))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/cache.py", line 37, in get
    value = getter_func()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/prompt_toolkit/layout/controls.py", line 239, in <lambda>
    cli.render_counter, lambda: self.get_tokens(cli))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyInquirer/prompts/rawlist.py", line 89, in _get_choice_tokens
    tokens.append((T, '  Answer: %d' % self.choices[self.pointer_index][0]))
IndexError: list index out of range

Environment:

OS: macOS 10.14 Mojave

$ python3 --version
Python 3.6.1
$ python3 -m pip show PyInquirer
Name: PyInquirer
Version: 1.0.2
Summary: A Python module for collection of common interactive command line user interfaces, based on Inquirer.js
Home-page: https://github.com/CITGuru/PyInquirer/
Author: Oyetoke Toby
Author-email: oyetoketoby80@gmail.com
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
Requires: prompt-toolkit, Pygments, regex
Required-by:
CITGuru commented 5 years ago

Hmmnnn the error is coming from the choices given. Thats a bad format of choices. Try and removing the default key, it still outputs the same error. And the default must be the index of the value in the given choices.

jmetzz commented 5 years ago

you can not use dictionaries as choices when the type is rawlist. The error doesn't happen if you actually use a simple list. However, the default configuration still doesn't make any difference. See code bellow.

answer = prompt([{
    'type': 'rawlist',
    'name': "test-question",
    'message': "Select item",
    'default': 2,
    'choices': ["Item 1", "Item 2", "Item 3", "Item 4"]
}])

print(answer)

In all my tests the default option only works with questions of {type: 'confirm'}

inyerade commented 3 years ago

default do not work in list and rawlist present in PyInquirer==1.0.3