Repository navigation
Description
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:
- Should we limit variable base name to only alpha numeric characters and underscore? In other words, should something like
$! als lkjasbe considered a variable with base name! als lkjator 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. - Should this syntax support item access like
$list[0]and$dict[key][nested]? I believe it should. - 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
pekkaklarck commentedon Mar 7, 2023
We already support the
$varsyntax in expression evaluated in Python (#2040) likeThis 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$varand${var}in this case, but that difference exists already nowadays. The$varsyntax typically works better in this usage, so people getting more used to it would mainly be positive.mkorpela commentedon Mar 7, 2023
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.I think this would give a lot of room to move in the future syntax.
This seems to work pretty nicely based on my experiments. Even something like
$dict[My funny key to an array][0]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
Evaluateis pretty easy also and more explicitly points to the keyword that explains that it is calling Python.This one might need to then change to explicit escaped.
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.
pekkaklarck commentedon Mar 7, 2023
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
$varsyntax wouldn't cause backwards incompatibility issues but it would be somewhat inconsistent. More importantly, not being able to use$object.attributewould be pretty annoying in many use cases and I'd miss$SPACE * 10as well.One possibility would be selecting what extended syntax we want to support and implement it ourselves without direct Python evaluation. Both
$object.attributeand$SPACE * 10would be pretty easy to handle, but there could also be more complicated expressions that would be useful. We could experiment this with the$varsyntax 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.pekkaklarck commentedon Mar 7, 2023
My point in the earlier comment about
IF $var > 0was that this syntax should work the same way as nowadays (i.e.$varisn't resolved before evaluation) even if we'd add$varsyntax as alternative for${var}. That means$varand${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 withIFandWHILE(and probably also withRun Keyword If) compared to normal keywords. That shouldn't be too hard to implement.We could also consider prohibiting using
${var} > 0syntax 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.manykarim commentedon Mar 8, 2023
Just a question:
Could this change also affect Robot Framework Libraries which use the
$in their syntax?I recently started e.g. with the
RESTinstanceLibrary and it uses Keywords and Arguments likeOutput $.emailto get the
emailkey 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?
iot40-dp commentedon Mar 8, 2023
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
$varis interpreted like${var}: As string. This introduces inconsistency in python evaluation like with the keyword Evaluate. In this case,$varreferences 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$varis 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.pekkaklarck commentedon Mar 8, 2023
Very good question @manykarim. This is related to the following open design decision I mentioned earlier:
When writing that, my thinking was that we shouldn't limit the name, but that would then mean that
$.emailwould 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\$.emailwould 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:$foosame as${foo}or just a literal string. I don't think this is a good idea.$varsyntax to start with an alpha numerical character. Then$foowould be a variable but$.fooand$['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$\nwould work or should we require curlies with them?$var.attrand$var * 10as extended variable syntax.pekkaklarck commentedon Mar 8, 2023
Making
$varvariable 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.
pekkaklarck commentedon Mar 8, 2023
@iot40-dp,
${var}isn't always a string. When you use it on its own as an argument likethe 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 likewhen 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
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 integer1the expression would be1 == 0which is valid (but obviously false), but if the value would be stringxyz, then the expression would bexyz == 0which 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 == 0already 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$varto valid Python identifier (it becomesRF_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$varbehave very differently. The former syntax often causes strange problems, so we generally recommend the latter. If we add the general$varsyntax, 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
$varsyntax that is generally equivalent to${var}, it is confusing if$varand${var}anyway work differently when evaluating expressions. On the other hand, if introducing$varmeans 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$varhas more benefits than problems, but if there are more people who are against it, I promise to listen.pekkaklarck commentedon Mar 8, 2023
One way to avoid the ambiguity with
$varand${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.6 remaining items
pekkaklarck commentedon Sep 21, 2023
@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$42and$3.14. In addition to that, when evaluating expressions$var > 0and${var} > 0will continue to be evaluated differently. In the latter case${var}is replaced before evaluation while in the former$varis 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: Secretwith library keywords, but it wouldn't help if$passwordis passed via user keywords.MoreFamed commentedon Sep 26, 2023
@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$varthe same way: the way it handles$varnow (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.
pekkaklarck commentedon Sep 27, 2023
If you have a variable
${x}that contains a string1, you can nowadays use${x} > 0and it works because Python will see and evaluate1 > 0. If we changed the logic so that variable replacement wouldn't be done before evaluation, Python would get"1" > 0which 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.
MoreFamed commentedon Oct 12, 2023
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 > $bwhen $a is a string10and $b is a string9nowadays. 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. :(Accept `=` with VAR.
pekkaklarck commentedon Oct 23, 2023
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$varsyntax 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.
sebastianciupinski commentedon Nov 2, 2023
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?
pekkaklarck commentedon Nov 2, 2023
Could you clarify your worries?
The change will affect usages like
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.
MoreFamed commentedon Nov 2, 2023
@pekkaklarck: As regards problems described here and in #4909: I like to suggest to support variables
$varin a different way than we thought up to now:$var, but@var,&varand%varas well to be recognized as variables by PythonThese two things together
${var}syntax possible where needed because of an ambiguity, performance or another reason.When this is implemented, the
$varsyntax, 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.BrachystochroneSD commentedon Feb 15, 2024
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:
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.
sebastianciupinski commentedon Nov 27, 2024
@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:
in new syntax
or f-strings like assignment will stop work and concatenate keyword need to be used all the time?