From cdb6b1dcdd9b7a546dc1f3f1fc98ffe5f8c075f8 Mon Sep 17 00:00:00 2001 From: John Chilton Date: Mon, 7 Oct 2024 09:59:19 -0400 Subject: [PATCH] Refactor text wrapper default handling to expose fewer internals. I don't want to have to set optionality inferred for all tool parameters and I want to try to encapsulate this logic more in the parameter definition. --- lib/galaxy/tools/parameters/basic.py | 13 ++++++++++++- lib/galaxy/tools/wrappers.py | 16 ++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/galaxy/tools/parameters/basic.py b/lib/galaxy/tools/parameters/basic.py index f79123002794..b238bf08c058 100644 --- a/lib/galaxy/tools/parameters/basic.py +++ b/lib/galaxy/tools/parameters/basic.py @@ -188,7 +188,6 @@ def __init__(self, tool, input_source, context=None): self.hidden = input_source.get_bool("hidden", False) self.refresh_on_change = input_source.get_bool("refresh_on_change", False) self.optional = input_source.parse_optional() - self.optionality_inferred = False self.is_dynamic = False self.label = input_source.parse_label() self.help = input_source.parse_help() @@ -351,6 +350,7 @@ def parse_name(input_source): class SimpleTextToolParameter(ToolParameter): def __init__(self, tool, input_source): input_source = ensure_input_source(input_source) + self.optionality_inferred = False super().__init__(tool, input_source) optional = input_source.get("optional", None) if optional is not None: @@ -404,6 +404,7 @@ class TextToolParameter(SimpleTextToolParameter): def __init__(self, tool, input_source): input_source = ensure_input_source(input_source) super().__init__(tool, input_source) + self.profile = tool.profile self.datalist = [] for title, value, _ in input_source.parse_static_options(): self.datalist.append({"label": title, "value": value}) @@ -419,6 +420,16 @@ def validate(self, value, trans=None): ): return super().validate(value, trans) + @property + def wrapper_default() -> Optional[str]: + """Handle change in default handling pre and post 23.0 profiles.""" + profile = self.profile + legacy_behavior = (profile is None or Version(str(profile)) < Version("23.0")) + default_value = None + if self.optional and self.optionality_inferred and legacy_behavior: + default_value = "" + return default_value + def to_dict(self, trans, other_values=None): d = super().to_dict(trans) other_values = other_values or {} diff --git a/lib/galaxy/tools/wrappers.py b/lib/galaxy/tools/wrappers.py index 01e8e8491bbc..34e5b84737e2 100644 --- a/lib/galaxy/tools/wrappers.py +++ b/lib/galaxy/tools/wrappers.py @@ -19,7 +19,6 @@ Union, ) -from packaging.version import Version from typing_extensions import TypeAlias from galaxy.model import ( @@ -33,7 +32,10 @@ from galaxy.model.metadata import FileParameter from galaxy.model.none_like import NoneDataset from galaxy.security.object_wrapper import wrap_with_safe_string -from galaxy.tools.parameters.basic import BooleanToolParameter +from galaxy.tools.parameters.basic import ( + BooleanToolParameter, + TextToolParameter, +) from galaxy.tools.parameters.wrapped_json import ( data_collection_input_to_staging_path_and_source_path, data_input_to_staging_path_and_source_path, @@ -126,15 +128,9 @@ def __init__( profile: Optional[float] = None, ) -> None: self.input = input - if ( - value is None - and input.type == "text" - and input.optional - and input.optionality_inferred - and (profile is None or Version(str(profile)) < Version("23.0")) - ): + if value is None and input.type == "text": # Tools with old profile versions may treat an optional text parameter as `""` - value = "" + value = cast(TextToolParameter, input).wrapper_default() self.value = value self._other_values: Dict[str, str] = other_values or {}