Skip to content

New RETURN statement for returning from user keywords #4078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pekkaklarck opened this issue Sep 6, 2021 · 11 comments
Closed

New RETURN statement for returning from user keywords #4078

pekkaklarck opened this issue Sep 6, 2021 · 11 comments

Comments

@pekkaklarck
Copy link
Member

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
@pekkaklarck
Copy link
Member Author

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.

@pekkaklarck
Copy link
Member Author

pekkaklarck commented Sep 6, 2021

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.

@pekkaklarck
Copy link
Member Author

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.

@MoreFamed
Copy link

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

@bhirsz
Copy link
Contributor

bhirsz commented Oct 6, 2021

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.

@pekkaklarck
Copy link
Member Author

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.

@bhirsz
Copy link
Contributor

bhirsz commented Oct 8, 2021

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

pekkaklarck added a commit that referenced this issue Oct 31, 2021
Documentation missing but ought to be otherwise mostly ready.
pekkaklarck added a commit that referenced this issue Nov 3, 2021
- Mention, and recommend, RETURN in "Return From Keyword (If)" docs.
- Enhance error message if RETURN used in test inside IF or FOR.
- Fix "Return From Keyword (If)" tests after message changed.
- Change messages used by "Continue/Exit For Loop (If)". They needed
  to be changed anyway when adding BREAK and CONTINUE (#4079).
pekkaklarck added a commit that referenced this issue Nov 4, 2021
@pekkaklarck pekkaklarck changed the title New RETURN statement for returning from a user keyword New RETURN statement for returning from user keywords Nov 18, 2021
@sebastianciupinski
Copy link

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.

@bhirsz
Copy link
Contributor

bhirsz commented Nov 19, 2021

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

@pekkaklarck
Copy link
Member Author

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

pekkaklarck added a commit that referenced this issue Dec 20, 2021
- Include in output generated by Rebot.
- Add `source` and fix `lineno`.
- Handle (i.e. ignore) messages logged by listeners.
pekkaklarck added a commit that referenced this issue Jan 10, 2022
- Add TRY/EXCEPT/FINALLY (#3075)
- Rename FOR ITERATION to ITERATION (fixes #4182)
- Add WHILE (#4084)
- Add RETURN (#4078)
- Add BREAK and CONTINUE (#4079)
pekkaklarck added a commit that referenced this issue Jan 11, 2022
Mainly to CONTINUE and BREAK (#4079) but also something for RETURN (#4078).
pekkaklarck added a commit that referenced this issue Jan 14, 2022
…lures.

Also fix using BREAK and CONTINUE directly under WHILE (i.e. not
nested in IF or TRY).
pekkaklarck added a commit that referenced this issue Jan 23, 2022
This makes it possible to preserve error messages when these
statements are used incorrectly as well as messages/keywords
created by listeners. Related to #4078 and #4079.
@Tim-QA
Copy link

Tim-QA commented Sep 27, 2024

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
Projects
None yet
Development

No branches or pull requests

5 participants