Skip to content

Support variables in format $var in addition to ${var} #4674

Description

@pekkaklarck

Curly braces required by Robot's variable syntax can be considered somewhat distracting and even ugly. Depending on the keyboard layout, writing them can also require annoying finger acrobatics. It would thus be convenient if it would be possible to use $var instead of ${var} and also @list, &dict and %env instead of @{list}, &{dict} and %{env}. For example, all these could be supported:

*** Variables ***
$VARIABLE        value

*** Test Cases ***
Example
    Log    $VARIABLE
    $var =    Keyword
    FOR    $item    IN    @stuff
        Log    $item
    END

In all the above cases the $var syntax is used on its own and that would work fine. This syntax wouldn't work too well when embedded to a string, though. For example, if you have variables $name and $named, string Hello, $name! could be parsed like Hello, ${name}!, but abc$named could be either abc${name}d or abc${named}. We could require using the ${var} syntax in cases where $var is ambiguous, that's how shell scripts work, but I believe it's better to require it always when variables are embedded. That makes the syntax more consistent and this approach is also considerably easier to implement. We can, however, look at this again later if the $var syntax is universally considered better and using it always (when possible) is considered important.

Supporting the $var syntax would be a backwards incompatible change, but if we support it only when variables are used on their own, it shouldn't be that bad. It would mean that all arguments starting with $, @, & and % would need to be escaped like \$not_var. We already require this with # (which otherwise starts a comment) so there's precedence for such syntax. Nevertheless, this would break lot of tests/tasks and the change can be made only in a major version. It would also be a good idea to deprecate using arguments starting with these characters already earlier. If this enhancement is considered important, it could be added in RF 7.0 and deprecation added in RF 6.2 or possibly already in RF 6.1.

Although the basic syntax would be simple, there are some design decision to be made:

  1. Should we limit variable base name to only alpha numeric characters and underscore? In other words, should something like $! als lkjas be considered a variable with base name ! als lkjat or should the whole thing be considered a literal string? Being more strict with the variable name would mean there's less need for escaping, but then these variables would work inconsistently compared to "normal" variables. It would also mean that some built-in variables like ${/} couldn't be used like $/. I got a feeling that it's better if we don't limit the name.
  2. Should this syntax support item access like $list[0] and $dict[key][nested]? I believe it should.
  3. Should this syntax support "extended variable syntax" like $var.upper() and $SPACE * 10? I don't have too strong opinion on this, but I guess it would be better to support it. Needing to use ${var.upper()} in this case would be a bit strange and inconsistent.

I'll add this issue tentatively to RF 7.0 scope, but we still need to make a bit more official decision about it. If we decide to include it, then another issue should be submitted about deprecating arguments starting with $ and other such variable meta characters.

Activity

  1. added this to the v7.0 milestone on Mar 7, 2023
  2. pekkaklarck commented on Mar 7, 2023

    @pekkaklarck
    MemberAuthor

    We already support the $var syntax in expression evaluated in Python (#2040) like

    IF    $var > 0    Log    This is positive!
    

    This is a special syntax that makes the variable available in the Python evaluation namespace. When the normal variable syntax is used like ${var} > 0, the variable is resolved before evaluating the expression. There's thus difference between $var and ${var} in this case, but that difference exists already nowadays. The $var syntax typically works better in this usage, so people getting more used to it would mainly be positive.

  3. mkorpela commented on Mar 7, 2023

    @mkorpela
    Member

    We could require using the ${var} syntax in cases where $var is ambiguous, that's how shell scripts work, but I believe it's better to require it always when variables are embedded.

    I think this is an excellent idea and very likely the most backwards compatible. It is much better to "template" with ${var} than with the {$var} proposed in other discussions.

    1. Should we limit variable base name to only alpha numeric characters and underscore?

    I think this would give a lot of room to move in the future syntax.

    1. Should this syntax support item access like $list[0] and $dict[key][nested]?

    This seems to work pretty nicely based on my experiments. Even something like $dict[My funny key to an array][0]

    1. Should this syntax support "extended variable syntax" like $var.upper() and $SPACE * 10?

    Jumping into Python is in my opinion a scary thing. I would recommend limiting it if possible. Even getting rid of it completely. Writing that line of Evaluate is pretty easy also and more explicitly points to the keyword that explains that it is calling Python.

    IF    $var > 0    Log    This is positive!
    

    This one might need to then change to explicit escaped.

    IF    \$var > 0    Log    This is positive!
    

    Or considered as an "ok" to have strings with unescaped $..
    Another solution might be to change the IF statement parsing logic to go inside the evaluated expression instead of now just passing it to python.

  4. pekkaklarck commented on Mar 7, 2023

    @pekkaklarck
    MemberAuthor

    Extended variable syntax has some pretty important use cases like ${object.attribute} and ${SPACE * 10}. These need to have some equally easy alternative before removing the extended variable syntax can be considered, and even then that would be a pretty badly backwards incompatible change.

    Not supporting extended variable syntax with the $var syntax wouldn't cause backwards incompatibility issues but it would be somewhat inconsistent. More importantly, not being able to use $object.attribute would be pretty annoying in many use cases and I'd miss $SPACE * 10 as well.

    One possibility would be selecting what extended syntax we want to support and implement it ourselves without direct Python evaluation. Both $object.attribute and $SPACE * 10 would be pretty easy to handle, but there could also be more complicated expressions that would be useful. We could experiment this with the $var syntax and then possibly later switch to this evaluation mode also with ${var} syntax. The main reason I'm not sure is this a good idea is that a custom expression parser is a lot more complicated solution than using Python evaluation. It could be worth the effort, though.

  5. pekkaklarck commented on Mar 7, 2023

    @pekkaklarck
    MemberAuthor

    My point in the earlier comment about IF    $var > 0 was that this syntax should work the same way as nowadays (i.e. $var isn't resolved before evaluation) even if we'd add $var syntax as alternative for ${var}. That means $var and ${var} wouldn't always be the same, but I don't think we can avoid this inconsistency. That would also mean that variables need to be handled differently with IF and WHILE (and probably also with Run Keyword If) compared to normal keywords. That shouldn't be too hard to implement.

    We could also consider prohibiting using ${var} > 0 syntax with IF and WHILE altogether and always require $var > 0. That would avoid common string comparison errors like ${var} == 'value', but it would also be pretty badly backwards incompatible. That change would require a separate issue but we already have somewhat related #4676.

  6. manykarim commented on Mar 8, 2023

    @manykarim
    Contributor

    Just a question:
    Could this change also affect Robot Framework Libraries which use the $ in their syntax?

    I recently started e.g. with the RESTinstance Library and it uses Keywords and Arguments like
    Output $.email
    to get the email key from the body of your last reponse.
    https://asyrjasalo.github.io/RESTinstance/#Output

    If I understand correctly, the $ here has nothing to do with a variable in that context.

    Would that cause a problem in your opinion?

  7. iot40-dp commented on Mar 8, 2023

    @iot40-dp

    I don't have knowledge about how interpretation of variables in robot framework works. I just know that ${var} is always a string. If it is an object or primitiv, it is the string representation of that object, number or whatever it is. Please correct me, if I am wrong.

    I would not do the proposed change if $var is interpreted like ${var}: As string. This introduces inconsistency in python evaluation like with the keyword Evaluate. In this case, $var references to the object/number/bool and ${var} to a string representation of the object. It is easy to understand that difference and it is very usefull. For somebody, who is new to Robot Framework and maybe also Python, it would be confusing that in some situation $var is a string representation of an object/number/bool and in other situations the object/number/bool itself. The big advantage of Robot Framework is, that it is EASY TO USE and EASY TO LEARN (also maybe for people without dev background). Do not make it complex because you don't want to type { and }. And not allowing $var in boolean expressions or in "Evalute" would remove something that is very useful.

  8. pekkaklarck commented on Mar 8, 2023

    @pekkaklarck
    MemberAuthor

    Very good question @manykarim. This is related to the following open design decision I mentioned earlier:

    Should we limit variable base name to only alpha numeric characters and underscore?

    When writing that, my thinking was that we shouldn't limit the name, but that would then mean that $.email would be considered the same as ${.email} and that would obviously break tests using that syntax. This is the reason we must deprecate this syntax. Using \$.email would work.

    The question is, how often is such syntax used and how big problems updating it would cause? It's likely RESTinsteance isn't the only library using JsonPath expressions which apparently always start with something like $. or $[, and there certainly are other places where $, @, & and % are used extensively. Things to consider:

    1. Don't care about this. Let users escape these special characters.
    2. Check is there a matching variable. Use it if there is, otherwise use the literal value. This is tempting and we've used similar approach earlier, but it has always caused issues. It's pretty annoying if you cannot know is $foo same as ${foo} or just a literal string. I don't think this is a good idea.
    3. Requite the base name of variables using the $var syntax to start with an alpha numerical character. Then $foo would be a variable but $.foo and $['foo'] wouldn't. We needed to decide how to handle our built-in variables ${/}, ${:} and ${\n}, though. Should we special case them so that $/, $: and $\n would work or should we require curlies with them?
    4. Same as the above, but require the whole variable base name to consist of alpha numerical characters and underscore. This would have a benefit that we could always interpret $var.attr and $var * 10 as extended variable syntax.
  9. pekkaklarck commented on Mar 8, 2023

    @pekkaklarck
    MemberAuthor

    Making $var variable names more strict than ${var} variable names would mean that the syntax is inconsistent. I don't consider that too big a problem in this case. Stricter variable names have a benefit that we know that, for example, ${foo.bar()} uses the extended variable syntax and isn't a strangely named variable. We could thus separately decide to make also ${var} variable names more strict. That would be a backwards incompatible change requiring longish deprecation period, but as the long term plan it could make sense. We then needed to decide what to do with ${/}, ${:} and ${\n}, though. Probably they could be preserved as special cases.

    At the same time, we could decide to make variable names case, underscore and space sensitive. That would avoid lot of ambiguity and confusion, but it would probably be even bigger backwards incompatible change than making variable names more strict. Anyway, also that could be a good long term goal.

    Both of these changes are something that would require separate issues. If we agree they are good ideas, we could start deprecation process already now.

  10. pekkaklarck commented on Mar 8, 2023

    @pekkaklarck
    MemberAuthor

    @iot40-dp, ${var} isn't always a string. When you use it on its own as an argument like

    Keyword    ${var}
    

    the keyword gets whatever the ${var} points to and it could be an integer, a list, or a totally custom object. It's only when you embed a variable into a string like

    Keyword    Hello, ${var}!
    

    when the variable is turned into a string. In these cases we basically have template where variables are replaced.

    The situation with expression that are evaluated in Python is a bit different. When you use

    IF    ${var} == 0
    

    the variable is replaced before evaluation exactly like in the Hello, ${var}! case. This means that what's evaluated contains the string representation of the variable. For example, if ${var} would be an integer 1 the expression would be 1 == 0 which is valid (but obviously false), but if the value would be string xyz, then the expression would be xyz == 0 which isn't valid at all. To avoid these problems, we introduced the support to use actual variable values in the Python evaluation namespace like $var == 0 already in RF 2.9 (#2040). When that syntax is used, the variable isn't resolved before evaluation so the evaluated syntax will be $var == 0. This would be invalid Python, but our evaluation machinery converts the $var to valid Python identifier (it becomes RF_VAR_var) and then our custom evaluation namespace handles it during evaluation. Without going any more into the details, we can safely say that in this particular context ${var} and $var behave very differently. The former syntax often causes strange problems, so we generally recommend the latter. If we add the general $var syntax, the behavior with expressions absolutely shouldn't change. This requires special casing expressions in this regard, but that's not too hard.

    It's a valid point that if we add $var syntax that is generally equivalent to ${var}, it is confusing if $var and ${var} anyway work differently when evaluating expressions. On the other hand, if introducing $var means people get used to it and automatically use it also with expressions, that's typically better than using the ${var} syntax. My current thinking is that introducing $var has more benefits than problems, but if there are more people who are against it, I promise to listen.

  11. pekkaklarck commented on Mar 8, 2023

    @pekkaklarck
    MemberAuthor

    One way to avoid the ambiguity with $var and ${var} behaving differently in expressions would be disallowing using the latter. It would be a backwards incompatible change and would require a long deprecation period, but it could be a good end goal. It would need to get its own issue, though.

  12. 6 remaining items

  13. pekkaklarck commented on Sep 21, 2023

    @pekkaklarck
    MemberAuthor

    @MoreFamed I agree with pretty much everything you wrote. I'm not sure do we need to prohibit variables with names like $3dogs, though, but we certainly shouldn't allow variables to contain only numbers to avoid conflicts with our own $42 and $3.14. In addition to that, when evaluating expressions $var > 0 and ${var} > 0 will continue to be evaluated differently. In the latter case ${var} is replaced before evaluation while in the former $var is handled during evaluation. That's confusing but I'm afraid we cannot really do anything to it.

    @Snooz82 That definitely needs to be taken into account. Secret variables as proposed in #4537 could help. Alternatively we could support type hints like arg: Secret with library keywords, but it wouldn't help if $password is passed via user keywords.

  14. MoreFamed commented on Sep 26, 2023

    @MoreFamed

    In addition to that, when evaluating expressions $var > 0 and ${var} > 0 will continue to be evaluated differently. In the latter case ${var} is replaced before evaluation while in the former $var is handled during evaluation. That's confusing but I'm afraid we cannot really do anything to it.

    @pekkaklarck Please, try to explain why nothing can be done about it. I understand the difference in evaluation. I would like to suggest that all expressions will be passed to Python to evaluate it (would not it be too resource-consuming?). It would handle ${var} and $var the same way: the way it handles $var now (is this technically possible?). Python would convert the value to its text representation on its own wherever needed. But I am not sure about backward compatibility of this approach, I cannot see the whole impact.

    Would not it be possible at least to pass all expressions to Python? One of the most confusing points of using variables in RF is to distinguish where expressions are passed to Python (and I can write simply $a + 1, eg. in FOR IN RANGE) and where they are not (and I have to pass them explicitely, if needed, like $a = Evaluate $a + 1). I know I am starting a bit different topic, however, I cannot grasp the reasons of not evaluating everything the same way.

    I know my thoughts are not perfectly clear now. :/ As regards variables and their evaluation/replacing, I am always confused a bit.

  15. pekkaklarck commented on Sep 27, 2023

    @pekkaklarck
    MemberAuthor

    If you have a variable ${x} that contains a string 1, you can nowadays use ${x} > 0 and it works because Python will see and evaluate 1 > 0. If we changed the logic so that variable replacement wouldn't be done before evaluation, Python would get "1" > 0 which causes a TypeError. This would be a massively backwards incompatible change and I'm sure there would be other similar cases as well.

    Having a less confusing syntax for evaluating conditions would be great and perhaps we at some point can do something to it. It's anyway off topic related to this issue.

  16. MoreFamed commented on Oct 12, 2023

    @MoreFamed

    If you have a variable ${x} that contains a string 1, you can nowadays use ${x} > 0 and it works because Python will see and evaluate 1 > 0. If we changed the logic so that variable replacement wouldn't be done before evaluation, Python would get "1" > 0 which causes a TypeError. This would be a massively backwards incompatible change and I'm sure there would be other similar cases as well.

    Thanks! I wanted to suggest harnessing an implicit type conversion (done by RF before handling the expression to Python, or something like this) to solve the TypeError when processing that "1" > 0 but this would not work. :( The results of ${a} > ${b} differs from $a > $b when $a is a string 10 and $b is a string 9 nowadays. An attempt to treat them the way that both expressions yields the same results would inevitably bring vast backward incompatibility, although there is no TypeError. My suggestion seems to be a dead-end street. :(

  17. added a commit that references this issue on Oct 23, 2023
  18. pekkaklarck commented on Oct 23, 2023

    @pekkaklarck
    MemberAuthor

    It turned out this issue is too badly backwards incompatible to be done already in RF 7.0. We need to first deprecate using arguments starting with $ or other variable identifiers as literal values. That will be done in RF 7.0 (#4909) and the actual change to support $var syntax in addition to ${var} needs to wait for RF 8.0.

    The above commit was accidentally linked to wrong issue. The work related to this hasn't really started.

  19. modified the milestones: v7.0, v8.0 on Oct 23, 2023
  20. sebastianciupinski commented on Nov 2, 2023

    @sebastianciupinski

    I hope you are aware that this is going to break plenty of frameworks build on top of RF. Personally I do not see any positives. Instead of curly brackets, we will have escape characters. is the game worth the candle?

  21. pekkaklarck commented on Nov 2, 2023

    @pekkaklarck
    MemberAuthor

    Could you clarify your worries?

    The change will affect usages like

    Keyword    $something
    

    where the argument starts with one of $@&%. Using these characters inside arguments won't be affected.
    Are you afraid that there are lot of such usages?

    Notice that we have already realized that we need to deprecate the syntax first (#4909). The plan is to do that in RF 7, but it may be too complicated in the available time.

  22. MoreFamed commented on Nov 2, 2023

    @MoreFamed

    @pekkaklarck: As regards problems described here and in #4909: I like to suggest to support variables $var in a different way than we thought up to now:

    • to pass expressions at all reasonable places to Python (my 2nd article from Sept 27),
    • and moreover, to "translate" not only $var, but @var, &var and %var as well to be recognized as variables by Python

    These two things together

    • would make the code in your initial contribution possible (hopefully!),
    • pose no backward incompatibility, except for arguments starting with the special characters,
    • and would still make the ${var} syntax possible where needed because of an ambiguity, performance or another reason.

    When this is implemented, the $var syntax, which is considered to be special now, could become "a new normal" (it would work at vast majority of cases and is neater, so why not); and on the other hand, the "normal" ${var} syntax could then be recommended just for special occasions.

  23. BrachystochroneSD commented on Feb 15, 2024

    @BrachystochroneSD

    Will the new syntax make the variables case sensitive?

    I find it pretty confusing and counter intuitive to have case insensitive variables.
    Here is an example of "problem" it produces:

    *** Variables ***
    ${MY_VAR}    hello
    
    
    *** Keywords ***
    My Keyword
        [Arguments]    ${my_var}
        IF    $my_var == $MY_VAR                    # Always true 
            Log            ${my_var} is default
        ELSE
            Log            ${my_var} is NOT default
        END
    
    
    *** Test Cases ***
    My Test Case
        Log           ${MY_VAR}
        Log           ${my_var}
        Log           ${my var}
        My Keyword    hello            # log 'hello is default'
        My Keyword    goodbye          # wrongly log 'goodbye is default'

    Again, it's possible to workaround it with variable syntax like ${GLOBAL_MY_VAR}. Not a big deal. Just confusing.
    I do like the case insensitive property for keywords. But I don't see much benefits to keep that rules for variables.

  24. sebastianciupinski commented on Nov 27, 2024

    @sebastianciupinski

    @pekkaklarck

    Could you clarify your worries?

    some of them has been raised here and in #4909. moreover today I can simply use f-strings like syntax:

    VAR         ${prefix}        go
    VAR         ${prefix1}      begin
    VAR         ${prefix2}      start
    VAR         ${suffix}       end
    
    ${out}=    Set Variable      ${prefix}then${suffix}      #    ->  gothenend
    ${out1}=    Set Variable      ${prefix}1then${suffix}      #    ->  go1thenend
    ${out2}=    Set Variable      ${prefix2}then${suffix}      #  ->  startthenend
    
    

    in new syntax

    VAR         $prefix        go
    VAR         $prefix1      begin
    VAR         $prefix2      start
    VAR         $suffix      end
    
    ${out}=    Set Variable      $prefixthen$suffix      #    ->  will it still be gothenend?
    ${out1}=    Set Variable      $prefix1then$suffix     #    ->  will it be go1thenend or maybe beginthenend
    

    or f-strings like assignment will stop work and concatenate keyword need to be used all the time?

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions