magmax/python-inquirer

[BUG] Editor breaks when default is a json

adityasule opened this issue · 2 comments

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"'
>>>

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

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?