fix: serialize typed values in ChoiceListParameter - #3440
Open
winklemad wants to merge 1 commit into
Open
Conversation
ChoiceListParameter.serialize did self._sep.join(x), which raises TypeError for any non-str var_type (int/float) because str.join requires str elements. parse() produces var_type values via map(self._var_type, ...), so the parse/serialize round trip was broken and an int/float ChoiceListParameter was unusable -- instantiating a task with one throws during task_id computation. Serialize each value with str(), matching EnumListParameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ChoiceListParameter.serializejoins the raw values:But
str.joinrequires every element to be astr, whileChoiceListParameter.parseproduces values ofself._var_type(return self.normalize(map(self._var_type, values))). So withvar_type=int(orfloat),serializeraisesTypeError, and the parse/serialize round trip is broken by construction.Because
serializeis called fromTask.to_str_paramsduringtask_idcomputation, anint/floatChoiceListParameteris completely unusable — instantiating any task that uses one throws:intchoices are a documented, typed use case (test/mypy_test.py), and the siblingChoiceParameter(via the defaultstr(x)serializer) andEnumListParameter(self._sep.join([e.name for e in x])) both handle non-str values correctly — onlyChoiceListParameterjoins raw typed values.Fix
Serialize each value with
str(), matchingEnumListParameter:No change for the existing
var_type=strcase (str("1") == "1"); it restores the parse/serialize round trip forint/float.Testing
Added
test_choice_list_param_typed_serialize_parse, assertingintandfloatChoiceListParameterserialize and round-trip correctly, with thestrcase unchanged. It fails before the change (TypeError) and passes after;test/parameter_test.pypasses andruff check/ruff format --checkare clean.