form: force option type to 'select' if there's 'choices'

This commit is contained in:
axolotle 2023-04-28 17:24:11 +02:00
parent 3f417bb9b3
commit 3a5d353c4b
2 changed files with 20 additions and 10 deletions

View file

@ -1989,10 +1989,10 @@ def test_option_default_type_with_choices_is_select():
}
answers = {"some_choices": "a", "some_legacy": "a"}
options = ask_questions_and_parse_answers(questions, answers)
options, form = ask_questions_and_parse_answers(questions, answers)
for option in options:
assert option.type == "select"
assert option.value == "a"
assert form[option.id] == "a"
@pytest.mark.skip # we should do something with this example

View file

@ -1305,15 +1305,25 @@ class OptionsModel(BaseModel):
def options_dict_to_list(
options: dict[str, Any], optional: bool = False
) -> list[dict[str, Any]]:
return [
option
| {
"id": option.get("id", id_),
"type": option.get("type", "select" if "choices" in option else "string"),
"optional": option.get("optional", optional),
options_list = []
for id_, data in options.items():
option = data | {
"id": data.get("id", id_),
"type": data.get("type", OptionType.select if "choices" in data else OptionType.string),
"optional": data.get("optional", optional),
}
for id_, option in options.items()
]
# LEGACY (`choices` in option `string` used to be valid)
if "choices" in option and option["type"] == OptionType.string:
logger.warning(
f"Packagers: option {id_} has 'choices' but has type 'string', use 'select' instead to remove this warning."
)
option["type"] = OptionType.select
options_list.append(option)
return options_list
def __init__(self, **kwargs) -> None:
super().__init__(options=self.options_dict_to_list(kwargs))