Skip to content

New RETURN statement for returning from user keywords #4078

Closed

Description

@pekkaklarck

We currently have two ways to return from a user keyword:

  1. [Return] setting that defines what to return once the keyword has been executed.
  2. Return From Keyword keyword and its variants Return From Keyword If, Run Keyword And Return, Run Keyword And Return If.

This is problematic for various reasons:

  1. [Return] is more widely used but it has pretty bad limitations. Most importantly, it cannot be used conditionally with IF/ELSE structures.
  2. Using keywords solves the above problem but using keywords for something like this is awkward.
  3. It's not good to have multiple ways to solve the same problem.

My proposal is that we add separate RETURN statement that can be used to return from a user keyword. The statement itself should return unconditionally, but it would be usable in IF/ELSE. Example usages:

*** Keywords ***
Return at the end
    Some Keyword
    ${result} =    Another Keyword
    RETURN    ${result}

Return conditionally
    IF    ${condition}
        RETURN    Something
    ELSE
        RETURN    Something else
    END

Early return
    IF    ${not applicable}
        RETURN
    END
    Some Keyword
    Another Keyword

Activity

  1. added this to the v5.0 milestone on Sep 6, 2021
  2. pekkaklarck commented on Sep 6, 2021

    @pekkaklarck
    MemberAuthor

    If we add RETURN, then we should deprecate and remove the [Return] setting. Probably should wait at least until RF 5.1 before we emit an actual deprecation warning, but documentation should be updated to mention that RETURN is the way forward. External tools like RoboCop could warn about the deprecated usage even earlier and RoboTidy could possibly convert from [Return] to RETURN. Robot itself could probably make it an error to use both RETURN and [Return] in one keyword.

    I'm not sure what to do with Return From Keyword and its variants. I consider it more important to remove syntax that's not anymore needed than to remove keywords. In addition to that, conditional variables like Return From Keyword If make it possible to create shorter data than IF and RETURN combination. That said, it would probably be better to deprecate also all these to have only one way to return in the end. BuiltIn having less keywords wouldn't hurt either.

  3. pekkaklarck commented on Sep 6, 2021

    @pekkaklarck
    MemberAuthor

    A common use case when returning is to execute a lower level keyword and returning its return value. Because we cannot separate

    RETURN    value    # Return this value
    

    from

    RETURN    Keyword    # Run this keyword and return its result
    

    I think its better to just support returning values. If you need to run a keyword and return its return value, it needs to be done separately:

    ${result} =    Keyword    # Run this keyword
    RETURN    ${result}       # and return its result
    

    The underlying issue here is that same that it would sometimes be nice to pass keyword return values directly as argument, but we cannot really separate

    Keyword    argument    # Pass this argument to the keyword
    

    from

    Keyword    Another Keyword    # Run this keyword and pass its return value as argument
    

    either. We already have #3187 about adding some inline keyword call syntax and it should naturally work with RETURN as well.

  4. pekkaklarck commented on Sep 14, 2021

    @pekkaklarck
    MemberAuthor

    This enhancement requires changes on many places starting from the parser and continuing to the execution side. I explain here briefly what's needed on the parsing side and can write separate comment later about changes on the execution side. This information hopefully helps someone interested to contribute and same instructions apply also to BREAK and CONTINUE (#4079).

    All this new stuff is needed in parsing:

    1. New token type in tokens.py.
    2. New lexer in statementlexers.py. Logic very much the same as in e.g. EndLexer.
    3. New lexer needs to be used by appropriate higher level lexers in blocklexers.py. In practice relevant block lexers need to return the new statement lexer from their lexer_classes method.
    4. New parsing model object in statements.py. End statement can be used as an example.
    5. These changes needed to be tested using unit testes in utest/parsing. It would be good to add some tests for the lexer in test_lexer.py, but that file is so huge it's hard to see where these new tests would fi. The whole flow can be tested with tests in test_model.py and from_params method should be tested in test_statements.py.
  5. MoreFamed commented on Oct 6, 2021

    @MoreFamed

    If we will have inline IF syntax (#4093):

    • [Return] value -> RETURN value
    • Return From Keyword value -> RETURN value
    • Return From Keyword If condition value -> IF condition RETURN value
    • Run Keyword And Return kw args -> RETURN Run Keyword kw args
    • Run Keyword And Return If condition kw args -> IF condition RETURN Run Keyword kw args

    The stuff on the right side looks prettier to me and is always shorter, so I would deprecate all keywords and constructions on the left side. :)

  6. bhirsz commented on Oct 6, 2021

    @bhirsz
    Contributor

    If we will have inline IF syntax (#4093):

    • [Return] value -> RETURN value
    • Return From Keyword value -> RETURN value
    • Return From Keyword If condition value -> IF condition RETURN value
    • Run Keyword And Return kw args -> RETURN Run Keyword kw args
    • Run Keyword And Return If condition kw args -> IF condition RETURN Run Keyword kw args

    The stuff on the right side looks prettier to me and is always shorter, so I would deprecate all keywords and constructions on the left side. :)

    Note that [Return] does not return anything, it just set what variable should be returned at the end of keyword.

  7. pekkaklarck commented on Oct 7, 2021

    @pekkaklarck
    MemberAuthor

    Agreed, [Return] and all Return ... keywords should be deprecated. In RF 5.0 that deprecation will only be a note in the docs, though, because it would be annoying to get deprecation warnings in RF 5.0 from syntax that you must use with earlier. We can then consider deprecating these loudly in RF 5.1 or later. There's no big hurry in removing them either. It would be annoying to migrate from RF 4.1 to e.g. RF 5.2 if e.g. [Return] wouldn't work at all in the latter.

    I expect that RoboTidy and RoboCop can help in transition.

  8. bhirsz commented on Oct 8, 2021

    @bhirsz
    Contributor

    Yes, we will add deprecation warnings in Robocop (for 5.0) and add transformer in Robotidy for replacing all variants to RETURN

  9. added a commit that references this issue on Nov 4, 2021
  10. changed the title [-]New `RETURN` statement for returning from a user keyword[/-] [+]New `RETURN` statement for returning from user keywords[/+] on Nov 18, 2021
  11. sebastianciupinski commented on Nov 19, 2021

    @sebastianciupinski

    I am ok with adding RETURN, but I do not think removing [Return] should be considered. It is going to force rewrite thousands of already created resources.

  12. bhirsz commented on Nov 19, 2021

    @bhirsz
    Contributor

    I am ok with adding RETURN, but I do not think removing [Return] should be considered. It is going to force rewrite thousands of already created resources.

    I already have written transformer in robotidy autoformatting tool that handles it for you (it will be released before 5.0 together with other changes for 5.0). So it should'nt be the issue

  13. pekkaklarck commented on Nov 19, 2021

    @pekkaklarck
    MemberAuthor

    [Return] will eventually be removed but it's likely to have quite long deprecation period.

  14. added a commit that references this issue on Dec 20, 2021
  15. Tim-QA commented on Sep 27, 2024

    @Tim-QA

    In the latest release 2.1.b1, RIDE continues to use the fixed parameter Return Value under Settings. This corresponds to the value [Return] in the text editor. Robot Framework 7.1 shows then the error messages:
    The '[Return]' setting is deprecated. Use the 'RETURN' statement instead.
    Is there any adjustment to this?

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