magmax / python-inquirer

A collection of common interactive command line user interfaces, based on Inquirer.js (https://github.com/SBoudrias/Inquirer.js/)
MIT License
1.01k stars 97 forks source link

[BUG] Editor breaks when default is a json #272

Open adityasule opened 1 year ago

adityasule commented 1 year ago

version: 2.10.0

When default value for editor is a json, the question cannot be rendered. Here is an example reproducing the error

Python 3.10.8 (main, Oct 13 2022, 10:17:43) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> import inquirer
>>> questions=[inquirer.Editor("test", "This is a Question", default=json.dumps({"foo": "bar"}))]
>>> inquirer.prompt(questions)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/prompt.py", line 11, in prompt
    answers[question.name] = render.render(question, answers)
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/render/console/__init__.py", line 38, in render
    return self._event_loop(render)
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/render/console/__init__.py", line 48, in _event_loop
    self._print_header(render)
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/render/console/__init__.py", line 76, in _print_header
    default=render.question.default, color=self._theme.Question.default_color, normal=self.terminal.normal
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/questions.py", line 55, in default
    return self.answers.get(self.name) or self._solve(self._default)
  File "/Users/name/.local/share/virtualenvs/rihqt8cC/lib/python3.10/site-packages/inquirer/questions.py", line 80, in _solve
    return prop.format(**self.answers)
KeyError: '"foo"'
>>>
Cube707 commented 1 year ago

this stems from the fact that default will be stringifyed uppon Object-creation and than on rendering get a .format() applyed to it. This failes with json strings, as they contain curly brackets, which are interpreted for formating.

No reals solution to this right now

XDGFX commented 1 year ago

You might be able to escape the curly brackets by doubling them up as mentioned here. Alternatively, that response suggests using string.Template which might be a good alternative?