Skip to content

Inconsistent operation results when using implicit conversion #3100

Closed

Description

@baxxos

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.

Activity

  1. pekkaklarck commented on Feb 27, 2019

    @pekkaklarck
    Member

    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...).

  2. baxxos commented on Feb 28, 2019

    @baxxos
    Author

    @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).

  3. robooo commented on Feb 28, 2019

    @robooo

    @pekkaklarck could you point us to file/lines where this is happening?

  4. pekkaklarck commented on Feb 28, 2019

    @pekkaklarck
    Member

    OK, let me explain why this happens.

    1. Let's first see the succeeding case where ${x} is 1 and ${y} is 1.0 (strings or integers, doesn't matter here).
    2. When ${${x} + ${y}} is evaluated, Robot first replaces variables inside the variables.
    3. As a result Robot then starts to evaluate ${1 + 1.0}.
    4. Unless there is actually a variable with this exact name, Robot tries to evaluate this as extended variable.
    5. It tries to find a base variable starting from the beginning so that the variable name can contain only alphanumeric characters, underscore, and space.
    6. The base name it founds is 1 so it tries to find a variable ${1}.
    7. Numbers work as variables automatically, and ${1} value is 1 as an integer.
    8. Next the extended syntax is evaluated in Python basically like this:
      >>> var = 1
      >>> eval('var + 1.0')
      2.0
    9. 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.
    10. As a result the evaluation happens like this:
      >>> var = 1
      >>> eval('var.0 + 1')
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "<string>", line 1
          v.1 + 1
            ^
      SyntaxError: invalid syntax

    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.

  5. pekkaklarck commented on Feb 28, 2019

    @pekkaklarck
    Member

    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.

  6. pekkaklarck commented on May 9, 2019

    @pekkaklarck
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions