Skip to content

Problems with recommentation to use $var syntax if expression evaluation fails #5274

Closed

Description

@pekkaklarck

A common error when evaluating expressions with IF or otherwise is using something like

IF    ${x} == 'expected'
    Keyword
END

The resulting error message was enhanced in RF 6.1 (#4676) to recommend using the $x syntax, and what you get nowadays is something like this:

Invalid IF condition: Evaluating expression "xxx == 'expected'" failed: NameError: name 'xxx' is not defined nor importable as module

Variables in the original expression "${x} == 'expected'" were resolved before the expression was evaluated. Try using "$x == 'expected'" syntax to avoid that. See Evaluating Expressions appendix in Robot Framework User Guide for more details.

This works fine otherwise, but if the variable has been quoted and evaluation fails because the variable contains a matching quote or a newline. For example, if ${x} contains x'x and you use

IF    '${x}' == 'expected'
    Keyword
END

the recommendation part of the error is this:

Try using "'$x' == 'expected'" syntax to avoid that.

The above recommendation isn't correct, because '$x' would be treated as a literal string where $x has no special meaning. The correct recommendation would be this one without quotes around $x:

Try using "$x == 'expected'" syntax to avoid that.

Although this is only a recommendation in an error situation, the recommendation being wrong is somewhat severe. Luckily it is pretty easy to detect is the original variable quoted and remove quotes in that case.

Activity

  1. added this to the v7.2 milestone on Nov 21, 2024
  2. self-assigned this
    on Nov 21, 2024
  3. changed the title [-]Recommentation to use `$var` syntax should remove possible quoting from example[/-] [+]Problems with recommentation to use `$var` syntax if expression evaluation fails[/+] on Nov 21, 2024
  4. pekkaklarck commented on Nov 21, 2024

    @pekkaklarck
    MemberAuthor

    When creating tests for this problem, I notice that the are problem also if the expression contains more than one variable. For example, using an expression ${x} or ${y} when these variables contain strings gives us this invalid recommendation:

    Try using '$x or ${y} or $y' syntax to avoid that.

    I'll fix that at the same time.

  5. pekkaklarck commented on Nov 21, 2024

    @pekkaklarck
    MemberAuthor

    The latter problem was due to the code using

    example[-1:] += [...]

    when it was supposed to be

    example[-1:] = [...]

    Took some time to see the difference. This example demonstrates how it affected results:

    >>> x = [1, 2, 3]
    >>> x[-1:] += [4, 5, 6]
    >>> x
    [1, 2, 3, 4, 5, 6]
    >>> x = [1, 2, 3]
    >>> x[-1:] = [4, 5, 6]
    >>> x
    [1, 2, 4, 5, 6]
  6. added a commit that references this issue on Nov 22, 2024
    7bbda9a
  7. added theissue type on Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions