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
Inline Python evaluation support using ${{expression}} syntax #3179
The main functionality would be evaluating Python expression inline without using the Evaluate keyword. It must be possible to use variables inside the expression using the $var syntax similarly as Evaluate does (#2040).
Based on a quick prototype implementation isn't overly complicated. The main thing to decide is that do we want to allow taking external modules into use somehow (Evaluate does that) or should we import some modules automatically (Run Keyword If and others import os and sys).
Issue #3187 proposes adding somewhat similar inline keyword call syntax. These two features need to be designed together so that the actual syntax used with them is consistent. See #3187 for different syntax alternatives.
Some examples demonstrating functionality below. See also the already mentioned issue #3100 for a real life example.
*** Variables ***${NUMBER}${100}*** Test Cases ***Example
Log Today is ${{datetime.date.today()}}.
Log The lucky number is ${{random.randint(0, 100)}}.
Log Number is ${NUMBER} and the next number is ${{$NUMBER + 1}}.
Log Lists and dicts are often needed with REST APIs: ${{ {'id': 1, 'name': 'Example', children: [7, 9]} }}
Log Let's go crazy: ${{ {str(i): i for i in range($NUMBER)} }}.
These examples assume that some modules like datetime and random are automatically available in the expression evaluation namespace. What modules to add there, if any, is still to be decided. Comments about that, and this feature in general, are appreciated!
os, sys, math, random, datetime, platform (as proposed on Slack) do sound like good candidates. Perhaps there are more
Kindly add one point of reference "somewhere" (keep re-editing the issue description?), so that this discussion has a "current status" invariant somewhere, until it goes for a PR.
The current status is that the base functionality ought to be done. I need to review it once more and then I can commit and push it.
I still haven't made a decision about automatically included modules. At the moment there are none, but adding support for them isn't hard. Bigger question is what to include. The mentioned os, sys, math, random, datetime, time, platform and json are all good candidates, but should probably take a look at the standard library reference to see is there anything else that would be useful.
A possible problem with importing all those modules is that each import always takes some time and that can slow down execution start-up time a bit. We import most of the aforementioned modules already, though, and with them additional imports are cheap. Need to still think should these imports be done only when this new syntax is used.
Yet another question is do we want to support additional modules. It would make it possible to use standard library modules we don't import as well as custom or third party modules we cannot even automatically import. I don't know are these that important use cases, though, because you can anyway use Evaluate that supports custom modules or switch to using variable files or libraries instead.
If we were to support additional modules, we needed to decide the syntax. My first idea was to allow adding extra modules to the end like ${{ expression | modules }}, but that's problematic because | is a valid operator in Python. Could use some characters like :, but that needed to be done on the token level because any character can occur inside a string. Another possibility would be supporting some magic syntax like !module to mean that module needs to be imported. If we'd do that, we possibly didn't need to import any modules automatically and instead require usage like ${{!random.randint(100)}}.
The commit above contains the basic implementation without any support (automatic or explicit) for additional modules. You can experiment with it by installing the latest code directly from GitHub. Feedback is appreciated!
If we were to support additional modules, we needed to decide the syntax.
Since there are already a lot of used operators (and Pythonic references that you may want to avoid overloading), unless a better syntax comes, it could be a succint (but maybe weird looking):
${{M=os,sys,random random.randint(100)}}
# Mandatory space ^ with no comma before
It looks (clearly) as a Bash reference, because it is. Kind-of like setting environment variables for this command only.
The commit is a bit of a handful to read right now (self-tests are always perplexing to me); I will maybe try reading it some more appropriate time. I am not sure how were you planning to add defaults and/or a way to change the namespace (add/remove/explicitly define), but then the "bash" way is already familiar to enough of us.
M=os,sys,random looks strange when you see it the first time. Similar modules=os,sys would be more clear, and if it was the the end it would also look same as with Evaluate that has modules as an optional parameter:
This is definitely one option, although I'm not sure is a space alone enough as a separator between the expression and modules=.
If you look at the commit closely, you'll notice that namespaces and additional modules supported by the new evaluate_expression function are already used by the Evaluate keyword. I copied its old functionality to the new utility (and needed to add some code to support expressions in the variables section). Tests for this functionality are pretty dense but ought to show different ways how it can be used.
The reason I preferred modules=os,sys expression vs the expression modules=os,sys (or whatever separator scheme), is because parsing it would be more straightforward.
It would also look more Unix way of "chaining" multiple commands together e.g. timeout 1m docker exec cont_id bash -s "echo hello world".
I cannot think of counter-examples right now, but it seems a bit ... radical change to make. However, if it happens only at ${{ }} and not on the normal ${ }, then I guess it could be fine.
There's actually some truth about selenium.webdriver.Whatever causing problems. I comment that more in #3349 where we in general should move discussion about the automatic imports.
"It is not planned to update to 3.2", so I cannot honestly test it.
I was thinking to put together a dummy test tomorrow, and I didn't want to forget a possibly good enhancement request.
It's not really spamming but this is so easy to test that I'm surprised if you haven't done it. Even if you couldn't upgrade to 3.2, you could install it to a temporary virtual environment. Anyway, I guess this ought to work in settings as well.
I have just upgraded my application to use robotframework-3.2.2.jar. However, when I tried to use the python inline expression ${{}} in my test case, it failed to identify the expression.
e.g.
Invalid variable name '${{len('123')}'
Any idea with what's missing? some config missing?
Activity
pekkaklarck commentedon May 16, 2019
Should also support
@{{expression}}and&{{expression}}variants.pekkaklarck commentedon May 17, 2019
Issue #3187 proposes adding somewhat similar inline keyword call syntax. These two features need to be designed together so that the actual syntax used with them is consistent. See #3187 for different syntax alternatives.
pekkaklarck commentedon Oct 10, 2019
Some examples demonstrating functionality below. See also the already mentioned issue #3100 for a real life example.
These examples assume that some modules like
datetimeandrandomare automatically available in the expression evaluation namespace. What modules to add there, if any, is still to be decided. Comments about that, and this feature in general, are appreciated!aaltat commentedon Oct 10, 2019
jsoncould be useful too.stdedos commentedon Oct 11, 2019
os,sys,math,random,datetime,platform(as proposed on Slack) do sound like good candidates. Perhaps there are moreKindly add one point of reference "somewhere" (keep re-editing the issue description?), so that this discussion has a "current status" invariant somewhere, until it goes for a PR.
pekkaklarck commentedon Oct 13, 2019
The current status is that the base functionality ought to be done. I need to review it once more and then I can commit and push it.
I still haven't made a decision about automatically included modules. At the moment there are none, but adding support for them isn't hard. Bigger question is what to include. The mentioned
os,sys,math,random,datetime,time,platformandjsonare all good candidates, but should probably take a look at the standard library reference to see is there anything else that would be useful.A possible problem with importing all those modules is that each import always takes some time and that can slow down execution start-up time a bit. We import most of the aforementioned modules already, though, and with them additional imports are cheap. Need to still think should these imports be done only when this new syntax is used.
Yet another question is do we want to support additional modules. It would make it possible to use standard library modules we don't import as well as custom or third party modules we cannot even automatically import. I don't know are these that important use cases, though, because you can anyway use
Evaluatethat supports custom modules or switch to using variable files or libraries instead.If we were to support additional modules, we needed to decide the syntax. My first idea was to allow adding extra modules to the end like
${{ expression | modules }}, but that's problematic because|is a valid operator in Python. Could use some characters like:, but that needed to be done on the token level because any character can occur inside a string. Another possibility would be supporting some magic syntax like!moduleto mean thatmoduleneeds to be imported. If we'd do that, we possibly didn't need to import any modules automatically and instead require usage like${{!random.randint(100)}}.Inline Python evaluation support using `${{expression}}` syntax
pekkaklarck commentedon Oct 13, 2019
The commit above contains the basic implementation without any support (automatic or explicit) for additional modules. You can experiment with it by installing the latest code directly from GitHub. Feedback is appreciated!
stdedos commentedon Oct 13, 2019
Since there are already a lot of used operators (and Pythonic references that you may want to avoid overloading), unless a better syntax comes, it could be a succint (but maybe weird looking):
It looks (clearly) as a Bash reference, because it is. Kind-of like setting environment variables for this command only.
The commit is a bit of a handful to read right now (self-tests are always perplexing to me); I will maybe try reading it some more appropriate time. I am not sure how were you planning to add defaults and/or a way to change the namespace (add/remove/explicitly define), but then the "bash" way is already familiar to enough of us.
pekkaklarck commentedon Oct 13, 2019
M=os,sys,randomlooks strange when you see it the first time. Similarmodules=os,syswould be more clear, and if it was the the end it would also look same as withEvaluatethat hasmodulesas an optional parameter:This is definitely one option, although I'm not sure is a space alone enough as a separator between the expression and
modules=.If you look at the commit closely, you'll notice that namespaces and additional modules supported by the new
evaluate_expressionfunction are already used by theEvaluatekeyword. I copied its old functionality to the new utility (and needed to add some code to support expressions in the variables section). Tests for this functionality are pretty dense but ought to show different ways how it can be used.stdedos commentedon Oct 13, 2019
The reason I preferred
modules=os,sys expressionvs theexpression modules=os,sys(or whatever separator scheme), is because parsing it would be more straightforward.It would also look more Unix way of "chaining" multiple commands together e.g.
timeout 1m docker exec cont_id bash -s "echo hello world".5 remaining items
stdedos commentedon Oct 21, 2019
Found a potential issue? How about:
${chrome options} = Evaluate sys.modules['selenium.webdriver'].ChromeOptions() ... sys, selenium.webdriverpekkaklarck commentedon Oct 21, 2019
Why would you do that and not
selenium.webdriver.ChromeOptions()?stdedos commentedon Oct 21, 2019
That is a very good question! I have no idea 😅
Google finds so many examples for the former, but not the latter one.
Maybe it is not as important then.
pekkaklarck commentedon Oct 21, 2019
There's actually some truth about
selenium.webdriver.Whatevercausing problems. I comment that more in #3349 where we in general should move discussion about the automatic imports.Automatic module imports with Evaluate and friends
stdedos commentedon Feb 20, 2020
Is this supposed to work in
Documentationsections?Like
[Documentation] | Do ${thing} in ${{x.name for x in resources}}, and have it rendered appopriately?pekkaklarck commentedon Feb 20, 2020
Did you try?
stdedos commentedon Feb 20, 2020
"It is not planned to update to 3.2", so I cannot honestly test it.
I was thinking to put together a dummy test tomorrow, and I didn't want to forget a possibly good enhancement request.
Apologies if that ^^^ is spamming you
pekkaklarck commentedon Feb 20, 2020
It's not really spamming but this is so easy to test that I'm surprised if you haven't done it. Even if you couldn't upgrade to 3.2, you could install it to a temporary virtual environment. Anyway, I guess this ought to work in settings as well.
shanebishop commentedon Jan 3, 2021
Is this new feature documented somewhere in the Robot Framework documentation?
shanebishop commentedon Jan 3, 2021
My mistake, inline evaluation is documented here.
angus-she commentedon Mar 23, 2021
I have just upgraded my application to use robotframework-3.2.2.jar. However, when I tried to use the python inline expression ${{}} in my test case, it failed to identify the expression.
e.g.
Invalid variable name '${{len('123')}'
Any idea with what's missing? some config missing?