Skip to content

Support Python style **kwargs with user keywords using &{kwargs} syntax #1561

Closed

Description

@spooning

Originally submitted to Google Code by @pekkaklarck on 1 Nov 2013

Python keywords have supported kwargs since RF 2.8 (#1383) and dynamic libraries ought to get the support in RF 2.8.2 (#1500). Full powers of this handy new functionality cannot be used yet, though, because user keywords cannot take kwargs. This limitation is illustrated by the examples below.

First, let's assume we a keyword like below for running a some program:

*** Keywords ***
Run My Program
    [Arguments]    @{args}
    Run Process    myprog    @{args}    stdout=/tmp/stdout.txt

The above keyword uses Process.Run Process keyword, which takes optional configuration parameters as kwargs. Using the keyword is simple:

*** Test Cases ***
Example
    Run My Program                    # no argument
    Run My Program    arg1    arg2    # two arguments

The problem is that we cannot change the keyword to take optional configuration. The best we can do is this:

*** Keywords ***
Run My Program
    [Arguments]    ${stdout}=/tmp/stdout.txt    @{args}
    Run Process    myprog    @{args}    stdout=${stdout}

Unfortunately, running it like earlier won't work anymore:

*** Test Cases ***
Example
    Run My Program                      # this still works
    Run My Program    arg1    arg2      # ooops, ${stdout} got value arg1!!
    Run My Program    out.txt    arg    # works but is hard to understand

A solution to this problem is enhancing user keywords so that they can take free kwargs just like Python (and soon dynamic) keywords can. In practice the keyword could look something like this:

*** Keywords ***
Run My Program
    [Arguments]    @{args}    &{config}
    Run Process    myprog    @{args}    &{config}

and could be used like

*** Test Cases ***
Example
    Run My Program    arg1    arg2             # same as original
    Run My Program    arg    stdout=out.txt    # works and is clear

As the above example shows, a key to resolving this issue is having a new variable type that gets free keyword arguments and can be used to pass them further. It will naturally be a dictionary, and can be also used for other purposes (#1450). Will the new variable use &{name} syntax or something else is still open.

Changes needed to implement this issue are so large that it can only be done in a major release.

Activity

  1. added this to the 2.9 milestone on Jun 30, 2014
  2. guykisel commented on Aug 1, 2014

    @guykisel
    Contributor

    Suggestion: Since RF uses @{args} for the equivalent of Python *args, could RF use @@{args} for the equivalent of Python **kwargs?

  3. pekkaklarck commented on Aug 1, 2014

    @pekkaklarck
    Member

    It will likely be easier to implement this if we just use a single character specifier. Anyway, deciding about that part if issue #1450.

  4. self-assigned this
    on Feb 8, 2015
  5. pekkaklarck commented on Feb 8, 2015

    @pekkaklarck
    Member

    Trace logging &{kwargs} was implemented by 681c65e. This issue now waits for dict-variable branch to be merged to master and documentation in User Guide.

  6. changed the title [-]Support Python style `**kwargs` with user keywords[/-] [+]Support Python style `**kwargs` with user keywords using `&{kwargs}` syntax[/+] on Mar 27, 2015
  7. added a commit that references this issue on Apr 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions