Repository navigation
Description
Currently our argument conversion logic allows specifying only one possible type for each argument. There are, however, use cases where declaring that, for example, either a list or a string is accepted and Robot should then try out different conversions. With type hints the natural syntax to use is Union like arg: Union[list, str].
The basic logic with multiple possible types would be going through all types one-by-one, trying to convert using each type, and going to the next one if conversion fails. There are, however, plenty of design decisions to be made about different details:
-
Should we first check does the given argument match any of the possible types before doing conversion? If, for example, we have
arg: Union[str, int]and the given argument is an integer, should we pass it as is (it matches one of the possible types) or should we test conversion with possible types one-by-one? In the latter case we'd convert the integer to a string because the first possible type isstr. In this case checking does the argument match any of the types feels better.On the other hand, if we have
arg: Union[int, str]and call with a string2, checking does the given argument match any of the type would mean that we don't do any conversion because one of the types isstr. If we'd go through types one-by-one, we'd convert it to an integer becauseintis beforestr. In this case testing conversion one-by-one works better.We could also make it so that we test conversions one-by-one if the given argument is a string and otherwise first check does the argument match any of the types. That feels a bit inconsistent and hard to explain, though. I got a feeling that going through types one-by-one works best. The key thing to remember is that
strshould be the last type to avoid converting all arguments to strings. -
What to do if we don't recognize one of the possible types? I think we should test conversion with all recognized types that are before it but return the value as-is if no conversion succeeds. For example,
arg: Union[int, MyThing]would convert string2to an integer but stringxxxwould be left as-is. This would be consistent with how we handlearg: MyThing. -
How to support multiple types with the
@keyworddecorator? We could require using Union also with it, but then this syntax wouldn't be supported with Python 2 and needing to usetypingmay not always be desirable with Python 3 either. I think we could allow using a tuple to types like@keyword(types={'arg': (list, str)}as an alias for Union. -
If we support a tuple of types with
@keyword, should we also support it with type hints likearg: (list, str)? Type checkers probably won't like that but I see no harm us supporting it assuming it doesn't require extra work. -
How to handle
Noneas type? Thetypingmodule considersNonean alias forNoneTypein type hints and thus if there's and argument likearg: None, we actually getNoneTypeas the type becausetyping.get_type_hints()does conversion automatically. We probably want to do that same also with types specified using@keyword(types: {'arg': None}). When giving types using@keywordas a list,Nonecurrently means that the matched argument doesn't have type info and that probably cannot be changed.
Activity
Add type conversion support with Union #3738
Union conversion fine-tuning #3738
pekkaklarck commentedon Oct 26, 2020
After the above commits this is now mostly done. Missing tasks:
@keyword.Support type conversion agains multiple types using tuple
UG: Document argument conversion changes.
Add type conversion support with Union robotframework#3738
Union conversion fine-tuning robotframework#3738
Support type conversion agains multiple types using tuple
UG: Document argument conversion changes.
UG: Document that Union[str, int] convers all args to strings.
KotlinIsland commentedon Mar 18, 2021
@pekkaklarck Everything can be converted to a string, should we add a special case where if string is in the valid types, run that conversion last?
mkorpela commentedon Mar 18, 2021
There are similar other types such as boolean.
pekkaklarck commentedon Mar 18, 2021
We possibly could change this so that if the given argument is not a string, we only convert it if it isn't already one of the accepted types. Anyway, changes need a new issue and cannot be done before RF 4.1.