Introduce UserAttrFormWidget type

This commit is contained in:
c-bata
2023-04-13 19:31:54 +09:00
parent 5c43d96236
commit 6da6480edd
2 changed files with 19 additions and 14 deletions
+5 -8
View File
@@ -46,10 +46,7 @@ if TYPE_CHECKING:
"TextInputWidgetJSON",
{"type": Literal["text"], "description": Optional[str], "user_attr_key": Optional[str]},
)
UserAttrRefJSON = TypedDict(
"UserAttrRefJSON",
{"type": Literal["user_attr"], "key": str, "user_attr_key": Optional[str]},
)
UserAttrRefJSON = TypedDict("UserAttrRefJSON", {"type": Literal["user_attr"], "key": str})
FormWidgetJSON = TypedDict(
"FormWidgetJSON",
{
@@ -156,8 +153,6 @@ class ObjectiveUserAttrRef:
return {
"type": "user_attr",
"key": self.key,
# Set 'user_attr_key' to simplify the frontend source code.
"user_attr_key": self.key,
}
@classmethod
@@ -202,7 +197,9 @@ def register_objective_form_widgets(
) -> None:
if len(study.directions) != len(widgets):
raise ValueError("The length of actions must be the same with the number of objectives.")
if any(not isinstance(w, ObjectiveUserAttrRef) and w.user_attr_key is not None for w in widgets):
if any(
not isinstance(w, ObjectiveUserAttrRef) and w.user_attr_key is not None for w in widgets
):
warnings.warn("`user_attr_key` specified, but it will not be used.")
form_widgets: FormWidgetJSON = {
"output_type": "objective",
@@ -218,7 +215,7 @@ def register_user_attr_form_widgets(
widget_dicts: list[Union[ChoiceWidgetJSON, SliderWidgetJSON, TextInputWidgetJSON]] = []
for w in widgets:
if isinstance(w, ObjectiveUserAttrRef):
raise ValueError("ObjectiveUserAttrRef can't be specified in register_user_attr_form_widgets.")
raise ValueError("ObjectiveUserAttrRef can't be specified.")
if w.user_attr_key is None:
raise ValueError("`user_attr_key` is not specified.")
user_attr_keys.add(w.user_attr_key)
+14 -6
View File
@@ -157,20 +157,28 @@ type ObjectiveTextInputWidget = {
type ObjectiveUserAttrRef = {
type: "user_attr"
key: string
user_attr_key?: string
}
// TODO(kenshin): Rename this type to FormWidget or something.
type ObjectiveFormWidget =
| ObjectiveChoiceWidget
| ObjectiveSliderWidget
| ObjectiveTextInputWidget
| ObjectiveUserAttrRef
type FormWidgets = {
output_type: "objective" | "user_attr"
widgets: ObjectiveFormWidget[]
}
type UserAttrFormWidget =
| ObjectiveChoiceWidget
| ObjectiveSliderWidget
| ObjectiveTextInputWidget
type FormWidgets =
| {
output_type: "objective"
widgets: ObjectiveFormWidget[]
}
| {
output_type: "user_attr"
widgets: UserAttrFormWidget[]
}
type StudyDetail = {
id: number