Skip to content

Inline Python evaluation support using ${{expression}} syntax #3179

Closed

Description

@pekkaklarck

See #3100 for motivation and earlier discussion.

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

Activity

  1. added this to the v3.2 milestone on May 9, 2019
  2. pekkaklarck commented on May 16, 2019

    @pekkaklarck
    MemberAuthor

    Should also support @{{expression}} and &{{expression}} variants.

  3. pekkaklarck commented on May 17, 2019

    @pekkaklarck
    MemberAuthor

    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.

  4. pekkaklarck commented on Oct 10, 2019

    @pekkaklarck
    MemberAuthor

    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!

  5. aaltat commented on Oct 10, 2019

    @aaltat
    Contributor

    json could be useful too.

  6. stdedos commented on Oct 11, 2019

    @stdedos
    Contributor

    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.

  7. pekkaklarck commented on Oct 13, 2019

    @pekkaklarck
    MemberAuthor

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

  8. pekkaklarck commented on Oct 13, 2019

    @pekkaklarck
    MemberAuthor

    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!

  9. stdedos commented on Oct 13, 2019

    @stdedos
    Contributor

    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.

  10. pekkaklarck commented on Oct 13, 2019

    @pekkaklarck
    MemberAuthor

    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:

    ${result} =    Evaluate    expression    modules=os, sys
    Log    ${result}
    # vs
    Log    ${{ expression modules=os,sys }}
    

    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.

  11. stdedos commented on Oct 13, 2019

    @stdedos
    Contributor

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

  12. 5 remaining items

  13. stdedos commented on Oct 21, 2019

    @stdedos
    Contributor

    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.

    Found a potential issue? How about:

        ${chrome options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()
        ...                     sys, selenium.webdriver
  14. pekkaklarck commented on Oct 21, 2019

    @pekkaklarck
    MemberAuthor

    Why would you do that and not selenium.webdriver.ChromeOptions()?

  15. stdedos commented on Oct 21, 2019

    @stdedos
    Contributor

    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.

  16. pekkaklarck commented on Oct 21, 2019

    @pekkaklarck
    MemberAuthor

    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.

  17. stdedos commented on Feb 20, 2020

    @stdedos
    Contributor

    Is this supposed to work in Documentation sections?

    Like [Documentation] | Do ${thing} in ${{x.name for x in resources}}, and have it rendered appopriately?

  18. pekkaklarck commented on Feb 20, 2020

    @pekkaklarck
    MemberAuthor

    Did you try?

  19. stdedos commented on Feb 20, 2020

    @stdedos
    Contributor

    "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

  20. pekkaklarck commented on Feb 20, 2020

    @pekkaklarck
    MemberAuthor

    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.

  21. shanebishop commented on Jan 3, 2021

    @shanebishop

    Is this new feature documented somewhere in the Robot Framework documentation?

  22. shanebishop commented on Jan 3, 2021

    @shanebishop

    Is this new feature documented somewhere in the Robot Framework documentation?

    My mistake, inline evaluation is documented here.

  23. angus-she commented on Mar 23, 2021

    @angus-she

    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?

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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions