tmbo / questionary

Python library to build pretty command line user prompts ✨Easy to use multi-select lists, confirmations, free text prompts ...
MIT License
1.52k stars 87 forks source link

Recent Breaking Change to Choice Api #88

Closed BradenM closed 3 years ago

BradenM commented 3 years ago

A breaking change was made to the Choice object api at some point between questionary versions 1.6.0 and 1.8.0 when specifying the value parameter.

Previously, in v1.6.0, the expected behavior was for the selected choice to return the provided value= "as is" with no modifications.

However, in v1.8.0, anything passed to value is now type cast as a string, leading to breaking changes for applications expecting the returned value to be of the same type originally passed in to the value parameter. (See https://github.com/BradenM/micropy-cli/issues/184, the issue that led to finding this.)

Quick search through the commit history points to this commit: https://github.com/tmbo/questionary/commit/604c112224a9bd45a09eec40353d8618a494c98f from #14 as a likely culprit.

I'm not sure if Questionary follows semantic versioning or not, but if so that PR (if it is actually the culprit) probably should have bumped the project version up by a major version given that the change is not backwards compatible w/ existing projects using v1.6.0.

Questionary v1.6.0

Python 3.8.3 (default, May 22 2020, 23:45:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from questionary import Choice

In [2]: import questionary as prompt

In [3]: choices = [Choice("one", value=1)]

In [4]: prompt_ch = prompt.checkbox("Choose a value", choices=choices).ask()
? Choose a value  [one]

In [5]: prompt_ch
Out[5]: [1]

In [6]: type(prompt_ch[0])
Out[6]: int

In [7]: prompt.__version__
Out[7]: '1.6.0'

Questionary v1.8.0

Python 3.8.3 (default, May 22 2020, 23:45:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from questionary import Choice

In [2]: import questionary as prompt

In [3]: choices = [Choice("one", value=1)]

In [4]: prompt_ch = prompt.checkbox("Choose a value", choices=choices).ask()
? Choose a value  [one]

In [5]: prompt_ch
Out[5]: ['1']

In [6]: type(prompt_ch[0])
Out[6]: str

In [7]: prompt.__version__
Out[7]: '1.8.0'
tmbo commented 3 years ago

Thanks a lot for bringing this up, this seemed to have been a regression in 1.8.0. It wasn't an intentional change.

I've added a test for this as well as a fix. If the build passes, I'll tag a 1.8.1 release with a fix.