python-lsp-server: Hover should not show docstring of inferred type but help for hover position
Try the following script:
bar = 10
"""A variable named bar"""
bar
When using auto-complete while typing “bar” the documentation of the variable is resolved correctly. However, if I hover over “bar” it shows the documentation of int()
which is not helpful at all.
The current implementation of hover uses Script.infer. Using Script.help gives the expected hover content.
About this issue
- Original URL
- State: open
- Created 9 months ago
- Comments: 15 (6 by maintainers)
I agree, that this is more useful if no docstring is attached to the variable. However, if someone took the time to write a docstring for the variable I think it should be shown to the user.
Pyright for example shows both (result from a “textDocument/hover” request):
Note that the variable docstrings are not a language feature but the format is generally accepted (Jedi, sphinx, Pyright, and probably more tools support it).
For me using
BaseName#description
looks more like a hack, because it shows the full line of code assigned to the variable, which looks more like Visual Studio’s approach when you hover a macro in C/C++.From what I see in pyright, the definition is shown when the value is literal, no matter if it’s a constant or variable.
dict
andlist
don’t show its contents.Various pyright behavior
Same goes with constants.
I think it will be effective enough to show it in order like ‘type, value (if applicable), docstring (if exists)’.
I see. That makes sense. So the question is more, if we really want to show a definition lookup in the hover result.
I could be wrong, but I think the pyright results are types rather than definition lookups. I put some type inference into my PR but Jedi is less strict (does not use the type
Literal[<val>]
if a variable is constant).If we decide on showing a definition lookup we should answer the following questions:
BaseName#description
the correct way to get the definition?I am open to suggestions for enhancing my PR.
I don’t do that via
repr
, but viadescription
field ofBaseName
object.If this is done for builtins only, then it’s fine. I say it because trying to get values of objects using
repr
is a bad idea since many libraries out there have buggy or not performantrepr
’s