You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add return type to Libdoc specs and HTML output #3017
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.
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.
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.
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.
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.
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.
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]
@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.
With normal functions and methods, it is easiest to use -> TheType annotation.
When using the @keyword decorator, it is possible to add 'return': TheType into the types dictionary. The above approach is generally recommended, though.
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.
@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.
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.
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!
Activity
mawentao119 commentedon Apr 4, 2019
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.
pekkaklarck commentedon Aug 15, 2019
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.
anandaprakash1979 commentedon Apr 25, 2020
Hi @pekkaklarck I am interested in implementing this request.
Can I work on this?
pekkaklarck commentedon Aug 21, 2020
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.
pekkaklarck commentedon Aug 21, 2020
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.Snooz82 commentedon Sep 15, 2020
@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 ...isAnySo, 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.
JockeJarre commentedon Nov 20, 2020
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
48 remaining items
pekkaklarck commentedon Nov 16, 2023
@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.
[-]Add return type to libdoc output[/-][+]Add return type to Libdoc specs and HTML output[/+]Enhance testing or return types in Libdoc specs.
pekkaklarck commentedon Nov 16, 2023
There are three ways to specify the return type:
-> TheTypeannotation.@keyworddecorator, it is possible to add'return': TheTypeinto thetypesdictionary. The above approach is generally recommended, though.'return': TheTypeto the type dictionary returned byget_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.
Document specifying return type in the User Guide. #3017
added Return Types to libdoc.html
pekkaklarck commentedon Nov 22, 2023
@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.
JirkaVrbka commentedon Jul 23, 2024
Since Robot Framework itself does not have those documented (and thus LibDoc gives
"returnType": nullto keywords likeConvert 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_typesas follows: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)
pekkaklarck commentedon Jul 30, 2024
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
reto 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!