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
Include library and keyword source information in Libdoc spec files #3507
Provide which are the sources that generated a given .libspec file (when possible).
For instance, if a keyword was generated from a method in some Python file, I'd like to track that file for modifications to know that I have to regenerate the spec if that file changes... I know there are cases where this isn't possible, but when it is, it'd be nice to have it.
The problem with including source information is that it only makes sense with files on the machine where spec files (or HTML files) are generated. That said, I see it being a very important feature e.g. with editors. Some initial ideas about this below:
This functionality should be configurable with a command line option. When enabled, source would be added to XML spec files but shown also in HTML output files. Not sure what the option should be named, perhaps something like --includesource with a suitable short option.
I agree about all those points... some observations:
1/2/3. the use case is automatically generating those for libraries that don't have them, so, having the --includesource as an option passed from the client seems good (being default is not needed).
Having line numbers would be nice for a go to definition... some notes here:
the file for the main imported module should be there globally for the spec (as we want to regenerate the libspec if it changes).
there should be a source tied to each specific keyword -- it's not uncommon in Python to have the functions in one place and having the API be just a bunch of imports, so, we want the actual place of the function (it may be skipped if it matches the global filename).
With dynamic libraries, like SeleniumLibrary, keywords can locate in multiple files. It could be good option to have keyword specific location instead of library level location.
Thanks for comments @fabioz and @aaltat. We clearly need also keyword level source information. I think it could work like this:
Library can have source information as discussed earlier.
Each keyword can have source information in format path:lineno where both parts are optional. If path is not present, then the library level source can be used instead.
With Python based libraries using the static or hybrid API we can easily get the actual function/method implementing the keyword. From it we can get the source file with inspect.getsourcefile and line number with inspect.getsourcelines. We can do this only when using Libdoc and when getting source is explicitly requested. That avoids all possible performance problems.
With dynamic libraries we cannot get the actual method in a generic manner. This is a problem in general, but it is an especially big problem because SeleniumLibrary utilizes the dynamic API. We probably could special case libraries using the PythonLibCore like SL does, but that's not a fully generic solution. Alternatively we could add get_keyword_source method to the dynamic API and enhance PythonLibCore to support it. Do you have an opinion about this @aaltat?
The dynamic API supporting getting keyword source would mean that also JavalibCore could support it. Any comments about that @Hi-Fi?
I don't know could we get keyword source info from Java based libraries utilizing the static API. Need to investigate that but I don't consider it a blocker if that's not possible.
Not sure what we should do with the remote API. If the dynamic API is enhanced, we could also enhance the remote API but I'm not sure is it worth the effort at this point.
I don't see problems adding new method for dynamic library API. Should not be that difficult to implement in the python lib core.
I don't know about remote libraries, because I haven't used them that much. I could imagine that the remote library source code could be available somewhere in the local file system and it could be useful to jump to the keyword.
No, it's not hard to add to the APi or to the DynamicCore. Let's do that.
That means we need a new PythonLibCore release for SL to be able to use this feature and to benefit from go-to-definition with LSP. Not a huge task either.
One thing that just crossed my mind is that it should be possible for filenames to be relative too (if we want libraries to generate it when doing a release and distribute it along the sources, filenames need to stored relative to the .libspec location).
They should be relative to the location of the .libspec file (my expectation is that library users will generate it on the ci as a part of the release process and the .libspec file will be put in the place where it'll be distributed relative to other resources).
I think it should be the default (as it's what users that want to distribute the sources should do), whereas the absolute path is valid just for the current machine.
How would this work if you install a lib with pip? Should the lib contain the spec or should you download it separately? In the latter case, how can we known what should be the root dir?
What's in general the use case for distributing spec files if Libdoc can be used to generate them as needed?
Perhaps better to move further discussion to Slack and return her when we agree on the details.
I think that if you install with pip it should be distributed along.
As for the use case, I think there are 2 things:
Library discoverability
Right now it's hard to know which are the available libraries as they can appear to be plain Python modules (so, having a .libspec file at the PYTHONPATH would help a lot there -- right now, I just try to create a .libspec after the user imports it -- and I pre-create the bultin libraries -- so, I can't offer suggestions of the libraries to import unless it was already imported once and that file was opened and had a request for code-completion, which isn't ideal).
I think that it's a bit wasteful having to regenerate the .libspec at each user machine.
I did some measurements here: at my machine, it currently takes around 5 seconds to generate the docs for the libraries which are builtin... I'm not sure how long it would take for other libraries, but the main problem is that right now this relates to the first time a library is used (so, the first time it takes around 0.5 seconds more to generate the code completion and it feels quite sluggish in that case -- sure, it's cached afterwards, but that's a first bad impression... usually I'd give a timeout error if a code completion request takes 0.5 seconds).
This functionality was documented in the User Guide by 7212ed3 and some corner cases fixed by earlier commits. I guess we can consider this issue done.
Activity
pekkaklarck commentedon Mar 17, 2020
The problem with including source information is that it only makes sense with files on the machine where spec files (or HTML files) are generated. That said, I see it being a very important feature e.g. with editors. Some initial ideas about this below:
This functionality should be configurable with a command line option. When enabled, source would be added to XML spec files but shown also in HTML output files. Not sure what the option should be named, perhaps something like
--includesourcewith a suitable short option.Should also configurable programmatically.
I don't think it is a good idea to enable this automatically with
*.libspecfiles (Libdoc: Support*.libspecextension when reading library information from spec files #3491). It's easy to enable the functionality when needed and documentation is easier if there are no special cases. I'm not strongly against this, though.Source should be stored as an absolute path to avoid breaking it if the spec file is moved.
Should check could keywords also get line numbers. I would expect editors to benefit from this.
Need to think should we allow giving the source externally as well. If there are not concrete use cases now, this can easily be added later if needed.
fabioz commentedon Mar 17, 2020
I agree about all those points... some observations:
1/2/3. the use case is automatically generating those for libraries that don't have them, so, having the --includesource as an option passed from the client seems good (being default is not needed).
aaltat commentedon Mar 17, 2020
With dynamic libraries, like SeleniumLibrary, keywords can locate in multiple files. It could be good option to have keyword specific location instead of library level location.
pekkaklarck commentedon Mar 17, 2020
Thanks for comments @fabioz and @aaltat. We clearly need also keyword level source information. I think it could work like this:
path:linenowhere both parts are optional. Ifpathis not present, then the library level source can be used instead.pekkaklarck commentedon Mar 19, 2020
Prototyped this a bit and here are the findings:
With Python based libraries using the static or hybrid API we can easily get the actual function/method implementing the keyword. From it we can get the source file with inspect.getsourcefile and line number with inspect.getsourcelines. We can do this only when using Libdoc and when getting source is explicitly requested. That avoids all possible performance problems.
With dynamic libraries we cannot get the actual method in a generic manner. This is a problem in general, but it is an especially big problem because SeleniumLibrary utilizes the dynamic API. We probably could special case libraries using the PythonLibCore like SL does, but that's not a fully generic solution. Alternatively we could add
get_keyword_sourcemethod to the dynamic API and enhance PythonLibCore to support it. Do you have an opinion about this @aaltat?The dynamic API supporting getting keyword source would mean that also JavalibCore could support it. Any comments about that @Hi-Fi?
I don't know could we get keyword source info from Java based libraries utilizing the static API. Need to investigate that but I don't consider it a blocker if that's not possible.
Not sure what we should do with the remote API. If the dynamic API is enhanced, we could also enhance the remote API but I'm not sure is it worth the effort at this point.
aaltat commentedon Mar 20, 2020
I don't see problems adding new method for dynamic library API. Should not be that difficult to implement in the python lib core.
I don't know about remote libraries, because I haven't used them that much. I could imagine that the remote library source code could be available somewhere in the local file system and it could be useful to jump to the keyword.
pekkaklarck commentedon Mar 20, 2020
No, it's not hard to add to the APi or to the DynamicCore. Let's do that.
That means we need a new PythonLibCore release for SL to be able to use this feature and to benefit from go-to-definition with LSP. Not a huge task either.
fabioz commentedon Mar 20, 2020
One thing that just crossed my mind is that it should be possible for filenames to be relative too (if we want libraries to generate it when doing a release and distribute it along the sources, filenames need to stored relative to the
.libspeclocation).pekkaklarck commentedon Mar 20, 2020
Should they be relative to the library installation directory or where? Should this be the default behavior?
fabioz commentedon Mar 20, 2020
They should be relative to the location of the .libspec file (my expectation is that library users will generate it on the ci as a part of the release process and the .libspec file will be put in the place where it'll be distributed relative to other resources).
I think it should be the default (as it's what users that want to distribute the sources should do), whereas the absolute path is valid just for the current machine.
pekkaklarck commentedon Mar 20, 2020
How would this work if you install a lib with pip? Should the lib contain the spec or should you download it separately? In the latter case, how can we known what should be the root dir?
What's in general the use case for distributing spec files if Libdoc can be used to generate them as needed?
Perhaps better to move further discussion to Slack and return her when we agree on the details.
fabioz commentedon Mar 20, 2020
I think that if you install with pip it should be distributed along.
As for the use case, I think there are 2 things:
Library discoverability
Right now it's hard to know which are the available libraries as they can appear to be plain Python modules (so, having a .libspec file at the PYTHONPATH would help a lot there -- right now, I just try to create a .libspec after the user imports it -- and I pre-create the bultin libraries -- so, I can't offer suggestions of the libraries to import unless it was already imported once and that file was opened and had a request for code-completion, which isn't ideal).
I think that it's a bit wasteful having to regenerate the .libspec at each user machine.
I did some measurements here: at my machine, it currently takes around 5 seconds to generate the docs for the libraries which are builtin... I'm not sure how long it would take for other libraries, but the main problem is that right now this relates to the first time a library is used (so, the first time it takes around 0.5 seconds more to generate the code completion and it feels quite sluggish in that case -- sure, it's cached afterwards, but that's a first bad impression... usually I'd give a timeout error if a code completion request takes 0.5 seconds).
21 remaining items
Fix Libdoc on Python 2 when source info cannot be read. #3507
Libdoc: Document spec files getting source info. #3507
pekkaklarck commentedon Mar 28, 2020
This functionality was documented in the User Guide by 7212ed3 and some corner cases fixed by earlier commits. I guess we can consider this issue done.
Fix getting source info from decorated functions
Add source and lineno to internal lib and kw objects.
Libdoc: Store lib/kw source info to spec files. robotframework#3507
Windows fixes for Libdoc source info support (robotframework#3507)
Fix Libdoc on Python 2 when source info cannot be read. robotframewor…
Libdoc: Document spec files getting source info. robotframework#3507
Fix getting source info from decorated functions