You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent operation results when using implicit conversion #3100
In RF, I often use shortened expressions like ${{x} + 1} when passing arguments to keywords to save some lines of code. However, recently I've noticed behavior which seems to be rather inconsistent:
> ${x}= Set Variable 1
# ${x} = '1'
> ${y}= Set Variable 1.0
# ${y} = '1.0'
> Log To Console ${${x} + ${y}}
2.0
> ${x}= Set Variable 1.0
# ${x} = '1.0'
> ${y}= Set Variable 1
# ${y} = '1'
> Log To Console ${${x} + ${y}}
! keyword: Log To Console ${${x} + ${y}}
! Resolving variable '${1.0 + 1}' failed: SyntaxError: invalid syntax (<string>, line 1)
From what I can tell, the operation result seems to be somehow dependent on the order of operands. Is this a Python-specific thing that I'm not aware of, or some kind of internal Robotframework issue?
I know that explicit casting of variables to numbers would probably solve the issue, however, it defeats the purpose of doing such operations in one line.
Tested on Python 3.7.2 with Robotframework 3.1.1 using Robotframework-debuglibrary.
This is caused by how variables are evaluated and how the so called extended variable syntax and automatic number variables work. I don't have time to explain it more thoroughly now, but I leave this issue about and do it later. I'll also think could this possibly be fixed but it may not be easy.
The best solution would probably be having some native syntax for evaluating expressions like this. Something like ${{$x + $y}} or $[$x + $y] where internal $x and $y work like with Evaluate keyword might be handy (but somewhat cryptic...).
@pekkaklarck I'm fine with the brief explanation and fully I support the idea of some native syntax for this - it's a common practice to evaluate embedded expressions through the mustache syntax like {{ this }} in many current frameworks (even though it's somewhat cryptic).
Let's first see the succeeding case where ${x} is 1 and ${y} is 1.0 (strings or integers, doesn't matter here).
When ${${x} + ${y}} is evaluated, Robot first replaces variables inside the variables.
As a result Robot then starts to evaluate ${1 + 1.0}.
Unless there is actually a variable with this exact name, Robot tries to evaluate this as extended variable.
It tries to find a base variable starting from the beginning so that the variable name can contain only alphanumeric characters, underscore, and space.
The base name it founds is 1 so it tries to find a variable ${1}.
Numbers work as variables automatically, and ${1} value is 1 as an integer.
Next the extended syntax is evaluated in Python basically like this:
>>>var=1>>>eval('var + 1.0')
2.0
When ${x} is 1.0 and ${y} is 1, things mostly work the same way. The problems is that base name of the extended variable is 1 also in this case, not 1.0 because . is not allowed in the name.
It's actually pretty much a co-incident that the first example works. We also cannot simply change extended variable evaluation to allow . in the name because the main usage of the extended variable syntax is to allow usage like ${object.attribute}.
As I commented earlier, it would be best to have a dedicated syntax for inline evaluation. The "mustache syntax" like ${{$x + $y}} would probably be best as it's used by in many other (templating) languages as well. It is somewhat cryptic but a counterargument is that also the extended variable syntax can be cryptic (see above...) and nobody needs to use this syntax. Another problem is that such new syntax would potentially cause backwards-incompatibility problems, but I doubt such problems would be that big and introducing this kind of a feature in a major release would be OK. Anyone interested to look at implementing this? Shouldn't be overly complicated.
Changes would probably be mostly limited to these places:
VariableSplitter should understand that if a variables starts with ${{ it also must end with }}. It should also handle cases like ${{${x} > ${y}}} where there's actual }}} at the end. This ought to be relatively easy to test with unit tests. A problem is that this is pretty old code and the state machine isn't that well implemented. I wouldn't mind a larger rewrite but it should be done separately.
VariableFinder should understand that {{whatever}} should be evaluated in Python. As I commented sometime earlier, should use the same logic as Evaluate keyword to allow using variables themselves in Python like $x. Possible "normal variables" like ${x} are replaced already earlier so at this point we'd only see their string representation. The evaluation logic should thus probably be moved from BuiltIn to somewhere in utils.
Activity
pekkaklarck commentedon Feb 27, 2019
This is caused by how variables are evaluated and how the so called extended variable syntax and automatic number variables work. I don't have time to explain it more thoroughly now, but I leave this issue about and do it later. I'll also think could this possibly be fixed but it may not be easy.
The best solution would probably be having some native syntax for evaluating expressions like this. Something like
${{$x + $y}}or$[$x + $y]where internal$xand$ywork like withEvaluatekeyword might be handy (but somewhat cryptic...).baxxos commentedon Feb 28, 2019
@pekkaklarck I'm fine with the brief explanation and fully I support the idea of some native syntax for this - it's a common practice to evaluate embedded expressions through the mustache syntax like
{{ this }}in many current frameworks (even though it's somewhat cryptic).robooo commentedon Feb 28, 2019
@pekkaklarck could you point us to file/lines where this is happening?
pekkaklarck commentedon Feb 28, 2019
OK, let me explain why this happens.
${x}is1and${y}is1.0(strings or integers, doesn't matter here).${${x} + ${y}}is evaluated, Robot first replaces variables inside the variables.${1 + 1.0}.1so it tries to find a variable${1}.${1}value is1as an integer.${x}is1.0and${y}is1, things mostly work the same way. The problems is that base name of the extended variable is1also in this case, not1.0because.is not allowed in the name.It's actually pretty much a co-incident that the first example works. We also cannot simply change extended variable evaluation to allow
.in the name because the main usage of the extended variable syntax is to allow usage like${object.attribute}.As I commented earlier, it would be best to have a dedicated syntax for inline evaluation. The "mustache syntax" like
${{$x + $y}}would probably be best as it's used by in many other (templating) languages as well. It is somewhat cryptic but a counterargument is that also the extended variable syntax can be cryptic (see above...) and nobody needs to use this syntax. Another problem is that such new syntax would potentially cause backwards-incompatibility problems, but I doubt such problems would be that big and introducing this kind of a feature in a major release would be OK. Anyone interested to look at implementing this? Shouldn't be overly complicated.pekkaklarck commentedon Feb 28, 2019
Changes would probably be mostly limited to these places:
VariableSplitter should understand that if a variables starts with
${{it also must end with}}. It should also handle cases like${{${x} > ${y}}}where there's actual}}}at the end. This ought to be relatively easy to test with unit tests. A problem is that this is pretty old code and the state machine isn't that well implemented. I wouldn't mind a larger rewrite but it should be done separately.VariableFinder should understand that
{{whatever}}should be evaluated in Python. As I commented sometime earlier, should use the same logic asEvaluatekeyword to allow using variables themselves in Python like$x. Possible "normal variables" like${x}are replaced already earlier so at this point we'd only see their string representation. The evaluation logic should thus probably be moved fromBuiltInto somewhere inutils.pekkaklarck commentedon May 9, 2019
Decided to prototype inline evaluation and it seems pretty easy to implement. That's a great feature for RF 3.2 and submitted #3179 about it.
The original problem has already been explained and this issue can be closed.