Repository navigation
Description
RF 3.1 added automatic argument conversion with library keywords using function annotations (#2890), default values (#2932), and some other means. That has worked very well, and it would be nice if something like that would work also with variables defined in test data. We probably could re-use the underlying argument conversion logic which ought to make it relatively easy to implement this. We also got a similar request related to variables passed from the command line #2946.
Automatically converting numbers like 42 and 3.14 to actual integers/floats would be handy, even when we already support them as variables like ${42} and ${3.14}. Being able to use datatime objects, bytes, and other types supported by the argument conversion that don't have direct variable alternatives would naturally be even more useful (but also less commonly needed). I would expect this to be most useful with lists and dictionaries and especially with nested list and dicts that cannot easily be constructed in Robot's data.
My proposed syntax for this functionality is supporting "type postfix" like :int in variable names like this:
*** Variables ***
${COUNT:int} 42
${GOOD IDEA:bool} Yes # Converted to Boolean True
${ANIMALS:list} ['cat', 'dog', 'horse'] # Python syntax
${NESTED:dict} {'a': 1, 'b': [{'x': 2}, {'x': 3}]} # Great with JSONThis syntax is derived from Python's function annotation syntax and also the same syntax that was decided to be used with command line variables in #2946. Created variable names would naturally omit the type suffix and look like ${COUNT} and ${GOOD IDEA}.
In addition to being available in the variable section, the new syntax should also be available also when creating variable otherwise, including in keyword arguments:
*** Keywords ***
Example
[Arguments] ${count:int}
${nested:dict} = Set Variable {'a': ${count}, 'b': [{'x': 2}, {'x': 3}]}
Set Test Variable ${DATE:datetime} 2019-08-23 12:03:42There are some design decision still to be made like should ${X:dict} create a DotDict instance that allows ${X.key} syntax and how to handle possible backwards incompatibility issues. For most parts the design looks pretty good, though, and I think this would be a valuable addition to Robot Framework. If there's enough interest, we can consider adding this already to RF 3.2 scope. To show your interest, vote with 👍 or 👎 and/or add comments.
UPDATE: The final syntax is mostly the same as above, but there needs to be a space after the colon like ${COUNT: int}. The motivation is making the enhancement less backwards incompatible.
Activity
pekkaklarck commentedon Aug 23, 2019
This change is backwards compatible because it's possible that someone has used variables that contain
:in their name like${FOO:BAR}. If we always consider the part after the colon to be the type, then such usage won't work anymore. One possibility is checking is that part a recognized type, but there's still a risk that someone has a variable like${MY:LIST}. It would naturally be safest to deprecate using colons in variable names first, but that would mean that the actual conversion support needed to wait until RF 3.3. In my opinion the risk for this change causing problems is small enough and benefits of the new syntax are big enough to do the change already in RF 3.2.pekkaklarck commentedon Oct 16, 2019
If this enhancement is implemented, it makes issue #2699 pretty useless. The more I think about it, the more I like this one compared to that one. Mainly because this has so many other benefits than just supporting lists/dicts with embedded arguments.
The main questions is, when could this be implemented. RF 3.2 starts to be so much overdue that this issue cannot fit it unless someone else is interested to look at implementing it. RF 3.3. is possible, but also there are so many other high priority features that external help would be highly appreciated.
Variable type conversion in arguments (robotframework#3278)
Variable type conversion in arguments (robotframework#3278)
pekkaklarck commentedon Nov 21, 2019
We have PR #3385 implementing this so I'll add this tentatively to RF 3.2 scope. That release is so much overdue that I cannot make promises will this issue make into it, though.
pekkaklarck commentedon Dec 4, 2019
Unfortunately this is too much work for the already overdue RF 3.2.
Support embedded type in argument robotframework#3278
48 remaining items
Prohibit types in embedded arguments with library keywords (#5425)
Refactor
Fine-tune type conversion error message
FOR loop variable conversion
Command line variable conversion
pekkaklarck commentedon May 20, 2025
Using non-string variables from the command line had a separate issue #2946. That has now been implemented as well and this conversion functionality needs to be documented as part of this issue.
Initial documentation for variable comversion
pekkaklarck commentedon May 29, 2025
The current syntax to create an embedded argument with both a type and a custom regexps is
${name:pattern: type}. This is somewhat inconsistent with the general variable type syntax${name: type}and even more inconsistent with the CLI variable type syntaxname: type:value(#2946). I believe we should change the syntax to${name: type:pattern}and others on the #devel channel agreed.UG: General cleanup to Variables section
Change embedded argument syntax with type and regexp
Enhance variable type conversion documentation
pekkaklarck commentedon May 30, 2025
I enhanced documentation in 30d87e4. Unfortunately it isn't possible to see generated docs before the final release is out, but here are links to the most relevant places in the documentation source that ought to render somewhat well:
All kind of comments about the docs are welcome and can be written either to here to a thread I'll start about this on the #devel channel on our Slack. This issue can anyway be considered done already now.