Skip to content

Add return type to Libdoc specs and HTML output #3017

Closed

Description

@robinmackaij

With the support of type-hints Python signatures can give information on the type of returned values of a method. This could be used to improve the documentation generated by libdoc.

def my_func(foo: str, count: int) -> str: will give the following in the Argument column:
foo: str, count: int
This could be expanded to include the type of the returned value:
foo: str, count: int -> str (following the Python notation)
The header Arguments could be updated to Signature to reflect this additional information.

Things to consider (from Slack conversation):

  • Take into account user keywords that may have used [Return] setting
  • How to handle def(*, named_arg: str) -> int for forced named arguments methods. The resulting signature in the documentation (*, named_arg: str -> int) may confuse users that are not familiar with the new syntax to force named arguments. Note that the * cannot be omitted from the method signature since this would mean the method is no longer named-only.
  • How to handle types in a signature that are based on the typing module (typing.Union, etc.). Right now, they come up as typing.Union in the documentation, while the inclusion of the standard module (typing.) does not really give the user much information. Also, the more complex typing constructions (Optional, nested structures, etc.) could lead to very long and unusable documentation entries.

Activity

  1. mawentao119 commented on Apr 4, 2019

    @mawentao119

    I usually deal with arguments in function. Such as "myvar = arg[1].encode('utf-8')" or other convertions .
    If libdoc can generate [return] arguments, that will be more helpful to other members of the team.

  2. pekkaklarck commented on Aug 15, 2019

    @pekkaklarck
    Member

    Showing return type information in Libdoc output would certainly be useful. The biggest question is how to show that information. Options include:

    • Add new column. A drawback is that it takes some precious horizontal space with all keywords and most of the keywords don't have any return info to show. Could avoid this issue partly by not showing the column at all if its fully empty (we do that with keyword tags).

    • Show the info in the same column with arguments. There's lot of free space there at least with keywords that have longer documentation. The column could then be renamed to Signature but still needed to thing a bit how to actually show arguments and return values there.

    I think the latter solution is better but this can still be discussed. Anyone interested to implement this? Shouldn't be overly complicated and I'd be happy to help and review pull requests.

  3. added
    good first issue
    help wanted
    on Aug 15, 2019
  4. anandaprakash1979 commented on Apr 25, 2020

    @anandaprakash1979

    Hi @pekkaklarck I am interested in implementing this request.
    Can I work on this?

  5. pekkaklarck commented on Aug 21, 2020

    @pekkaklarck
    Member

    Sorry @anandaprakash1979 for missing your comment. Are you still interested in looking at this issue? RF 4.0 will contain also other LIbdoc HTML (e.g. #3586) and return value could be added at the same time.

    We could also think should return value info be added to Libdocs XML outputs (libspec). Do you @fabioz see that as a useful enhancement? If yes, we should submit a separate issue about it.

  6. added this to the v4.0 milestone on Aug 21, 2020
  7. pekkaklarck commented on Aug 21, 2020

    @pekkaklarck
    Member

    One thing to decide is how to handle user keywords that use the [Return] setting to specify the return value. That is the actual value, not type, so it's different information than what function annotations provide. Showing it would probably be useful anyway.

  8. Snooz82 commented on Sep 15, 2020

    @Snooz82
    Member

    @pekkaklarck

    For robot keywords it would be interesting that these keywords could return anything.
    So i would propose that the Return Type of robot keywords that has [Return] or Return From Keyword ... is Any

    So, for us (imbus) as user of the xml output, we would really be interested into getting an information if a keyword does return or does not return anything.
    What is returned is not the most important issue. If this information is there, fine. If not, also ok.

    i think also for Libdoc it would be a nice information.

  9. JockeJarre commented on Nov 20, 2020

    @JockeJarre
    Contributor

    As discussed on Slack, would be nice if the @Keyword decorator allow setting the return type.

    the proposed syntax:

    @Keyword(types={'return': int})

    It's compatible with how they are represented in func.annotations and would make implementation really easy. (Pekka)
    There are also thoughts of having support for Unions as return type. Like Union[str, int]

    Here is the whole slack thread https://robotframework.slack.com/archives/C0K0240NL/p1605819214419400?thread_ts=1604056554.147300&cid=C0K0240NL

  10. removed this from the v4.0 milestone on Nov 30, 2020
  11. 48 remaining items

  12. pekkaklarck commented on Nov 16, 2023

    @pekkaklarck
    Member

    @Snooz82 was able to convince me that this is an important functionality in RF 7.0. I implemented the core part of this functionality and now the return type is in Libdoc spec files. The remaining task is to show the type also in the Libdoc HTML outputs similarly as argument types. @Snooz82 himself promised to look at that.

  13. changed the title [-]Add return type to libdoc output[/-] [+]Add return type to Libdoc specs and HTML output[/+] on Nov 16, 2023
  14. pekkaklarck commented on Nov 16, 2023

    @pekkaklarck
    Member

    There are three ways to specify the return type:

    1. With normal functions and methods, it is easiest to use -> TheType annotation.
    2. When using the @keyword decorator, it is possible to add 'return': TheType into the types dictionary. The above approach is generally recommended, though.
    3. With dynamic libraries, the return type can be added as 'return': TheType to the type dictionary returned by get_keyword_types.

    We don't use inspection for trying to find the return type from Python keywords or from user keywords. With the former it is very easy for users to specify the type themselves and with the latter we couldn't get the type reliably anyway. Adding type hint support for user keywords is a topic for a separate issue.

  15. added a commit that references this issue on Nov 17, 2023
  16. pekkaklarck commented on Nov 22, 2023

    @pekkaklarck
    Member

    @Snooz82 added the return type information to Libdoc's HTML output and you can see how it works with the latest Browser library documentation. This enhancement will be part of the forthcoming RF 7.0 alpha 2 and then it's easier to test this also with other libraries. If you have enhancement ideas or other comments, let us know either here or on Slack.

  17. added
    acknowledge
    on Nov 22, 2023
  18. JirkaVrbka commented on Jul 23, 2024

    @JirkaVrbka

    We don't use inspection for trying to find the return type from Python keywords or from user keywords. With the former it is very easy for users to specify the type themselves and with the latter we couldn't get the type reliably anyway.

    Since Robot Framework itself does not have those documented (and thus LibDoc gives "returnType": null to keywords like Convert To Binary) I played with the code a little and found a solution to at least give information that a keyword actually returns anything (and giving 'Any' as return type) or not.

    In file argumentparser.py, we can change function _get_types as follows:

     def _get_types(self, method):
            # If types are set using the `@keyword` decorator, use them. Including when
            # types are explicitly disabled with `@keyword(types=None)`. Otherwise get
            # type hints.
            if isclass(method):
                method = method.__init__
            types = getattr(method, 'robot_types', ())
            if types or types is None:
                return types
            try:
                hints = get_type_hints(method)
    
                if 'return' not in hints:
                    has_return = True if re.search(r' *return (?!None).+', inspect.getsource(method)) else False
                    if has_return:
                        hints['return'] = typing.Any
    
                return hints
            except Exception:  # Can raise pretty much anything
                # Not all functions have `__annotations__`.
                # https://github.com/robotframework/robotframework/issues/4059
                return getattr(method, '__annotations__', {})
    

    Is it something that LibDoc could benefit from? What do you think @pekkaklarck ? If yes, I can create a PR from this POC.

    (Tested on BuiltIn and SeleniumLibrary)

  19. pekkaklarck commented on Jul 30, 2024

    @pekkaklarck
    Member

    I don't think it's worth the effort to try to parse return information from the source code. It would require something more than using re to get any reliable information. More importantly, this would slow down inspecting keyword information and thus could only be done with Libdoc, not when running tests. All this makes the enhancement pretty complicated compared to just adding return type information to keywords themselves. PRs adding such info to standard libraries would be appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

acknowledgealpha 2effort: mediumpriority: medium

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions