-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Value conversion for CategoricalParameter
and TaskParameter
#171
Comments
As discussed, the function works as intended with correct inputs, but can have improper validation in the demonstrated example, accepting improper input causing a downstream error. This is due to the automatic structuring of a string as list. If its possible to avoid this automatic conversion this loophole should be fixable. |
There is an ugly and a nice solution to this. The core of the problem is that we convert the input of values/active_values to tuple and once this is done, there is no way you can tell whether the original input was a string or any other iterable. That is:
So the only way to catch that first case is to do a check before the conversion happens. It attrs speech, that would mean before calling the converter or as part of the converter. So the "ugly" solution would be to write a converter utility that does the check and use it instead: def nonstring2tuple(x: Iterable) -> Tuple:
if isinstance(x, str):
raise ValueError("Input must not be a string.")
return tuple(x) This would only require to replace The "nice" solution would be to use a three-argument converter that also gets the respective context like the validators (i.e., not only the to-be-converted value but also |
@AdrianSosic I don't consider the second solution |
I've heard the new attrs version is around the corner (should've already happened before PyCon), meaning that the new |
CategoricalParameter
and TaskParameter
I experienced unexpected behaviour in the validation when using a string as input type for active_values in the TaskParamter instead of a list.
I created the TaskParameter with a single-char string for active_values, i.e.
active_value="A"
for testing purposes. The Campaign object could be created without a problem. Unexpectedly, I got an error when creating the Campaign object in case the string for active_values was longer than 1, for exampleactive_value="C12"
:As @Scienfitz already pointed out, it is related to the input type, i.e. list vs string, and that the string is interpreted as a list in the validation. Therefore
"C" is not in ["C12", ..]
fails whereas"A" is not in ["A", ..]
works in case of a single-char string. To provide robustness or a clearer error message in that case, the validation could check whether active_values is a list or string and act accordingly.When I input active_values as a list, i.e.
active_value=["C12"]
, the validation and creation of the Campaign object works without problems.The text was updated successfully, but these errors were encountered: