-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add return type to Libdoc specs and HTML output #3017
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
Comments
I usually deal with arguments in function. Such as "myvar = arg[1].encode('utf-8')" or other convertions . |
Showing return type information in Libdoc output would certainly be useful. The biggest question is how to show that information. Options include:
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. |
Hi @pekkaklarck I am interested in implementing this request. |
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 |
For robot keywords it would be interesting that these keywords could return anything. 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. i think also for Libdoc it would be a nice information. |
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) Here is the whole slack thread https://robotframework.slack.com/archives/C0K0240NL/p1605819214419400?thread_ts=1604056554.147300&cid=C0K0240NL |
We want RF 4.0 out soon and decided not to include this issue in it. If someone is really interested in it an provides a PR, this can still be considered, but most likely RF 4.1 is a better target. The biggest open question is the difference with return info we get from library keywords (type info) and from user keywords (actual value, often as a variable). |
Hi @pekkaklarck, I'm new here but I'd be interested to work on this. |
Sounds good. There are few related things to be done related to this:
|
Thanks, I've been looking at adding the return type to the Libdoc output regardless of the output format and it does not seem to be too difficult. Wouldn't it make more sense to show the expected return type (if it exists) regardless of the chosen Libdoc output format? Why only HTML? |
The comment only had HTML initially because I accidentally posted it too early.... |
There's some WIP implementation now in https://github.com/kollonqv/robotframework/commit/d2439b072b2720612e23bcc304ddac765824d4f5 We now show the type hint for return information for all Libdoc outputs. HTML: show:
However, the current WIP implementation processes the return type hint as an argument. This leads to the return type hint being described as an i.e. XML: <keywords>
<kw name="Test Return Type" lineno="25">
<signatures repr="foo: str, count: int, return: str">
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="foo: str">
<name>foo</name>
<type>str</type>
</arg>
<arg kind="POSITIONAL_OR_NAMED" required="true" repr="count: int">
<name>count</name>
<type>int</type>
</arg>
<arg kind="RETURN_TYPE" required="false" repr="return: str">
<name>return</name>
<type>str</type>
</arg>
</signatures>
<doc/>
<shortdoc/>
</kw>
</keywords> The current WIP approach definitely needs to be changed as type hint is not an argument. |
Hi great news, will test it out right away.
/Richard
|
Hi @JockeJarre, Sorry but it’s not ready yet, I’ve only tested this with annotation only as in the original description of the issue. There’s still work to do and I don’t know how it behaves with @Keyword, I’ve not looked into that yet |
@kalle
No problem,
If you need someone to help with testing, just let me know.
/Richard
|
Hi, i think we should not change again the XML structure. I know that return type is the last element in the type hints, in python but that representation makes imho no sense. |
@JockeJarre I checked and it seems to behave ok with the @Keyword as well so you can give it a spin if you want. I updated the HTML to separate the return type from the arguments and left the xml's untouched for now. I was wondering if it would be clearer to separate the return type under it's own title under Arguments if it exists or do we want to have them all under Signatures title and just add an empty line between the arguments and the return?
|
Hi,
yes it looks better separated!
Update, just tested it. It looks ok to me. Surprised it worked with so few changes.
/Richard
|
I need to do some fixes to the implementation and libdoc acceptance tests to make them all pass. I'll open up a PR on this once ready |
have you read my comment about the XML structure? i really think that it is absolutely not the correct way to add the return type to the arguments section or change the format again, for such a small "feature/reason". you can add a new node/field to xml and json without creating new incompatibility. cheers |
I think it is not that easy to detect if a python method/function has no return statement in it. and therefore i think it is something different between "not giving a return type hint" and "a hint to None" |
So in general we have imho two states in the libdoc: a) return type defined (None, str, Union[int, float], etc) |
I already did something like it. It is possible to detect via inspect. The code needs to be enumerated, sure, but I think is it better to go through that instead of just giving up. Here I would like to stress, that even RobotFramework libraries are not (currently) typed, so without walking through the code, this change will be useless |
@JirkaVrbka if you have a good proposal that:
Then i think it would be very nice. |
"does not need any external dependancies" Is inspect (part of python standard libraries) internal dependency? If it is, I can do that |
In my opinion it's enough to get the return type annotation and not look at the keyword code. Users who are interested to get return type listed can very easily add the needed annotation. That's the same as with type hints in general. Using just the annotation ought to make implementation pretty simple. I believe the execution side model objects that Libdoc uses could already get the return type, then it needed to be added to Libdoc's model, and finally written to specs and shown in HTML. |
We are just starting RF 7 planning. Is there someone interested to provide a PR for this in RF 7 timeframe (latest in November) or should we move this further? |
@pekkaklarck maybe i find time |
@pekkaklarck / @Snooz82 how do you feel about this one? could it be squeezed into RF 7? :) |
I doubt I have time to look at this myself in RF 7 timeframe, there are more important issues for me and only a limited amount of time, but I can review a PR if someone is interested to implement this. The first step would be agreeing on what exactly we want to include. I believe in RF 7 it would be enough to just look at Python annotations and forget about user keywords altogether. |
The core part of #3017. The final task is adding the information to the HTML outputs.
@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. |
Explicit tests for specifying the return type with dynamic libraries and with the `@keyword` decorator. Part of #3017.
There are three ways to specify the return type:
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. |
Since Robot Framework itself does not have those documented (and thus LibDoc gives In file
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) |
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 |
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):
[Return]
settingdef(*, 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.The text was updated successfully, but these errors were encountered: