3

I've been looking around to see if I can create the same behaviour for Python in VSCode to show string param inputs as you can do with jsdoc in JavaScript.

JavaScript example with JSDoc:

/**
* @method someMethod
* @description A special method.
* @param { "option1" | "option2" } param1 Choose an option.
*/
function someMethod(param1) {
    console.log(param1);
}

So when calling the method, VSCode will give auto completion options for param1.

enter image description here

So I'm looking for a Python equivalent, preferably using google docstring format:

def some_method(param1: str) -> None:
    """A special method.

    Args:
        param1 (str): Choose an option. # HOW CAN WE ADD INTELLISENSE OPTIONS HERE??
    
    """
    print(param1)
Steve
  • 4,372
  • 26
  • 37
  • first question is: does the python language server parse the doc-strings – rioV8 Feb 08 '21 at 11:15
  • There is `choices` option in NumPy Doc format, but I think VS Code will need some extra extension to deal with it. [doc](https://developer.lsst.io/python/numpydoc.html#choices) – Eugene K Feb 12 '21 at 07:35

1 Answers1

4

In VSCode, it is the language server used that determines autocompletion. The current 'gold standard' for Python completion is the Microsoft Pylance extension, which builds on Microsoft Pyright to analyse your source code.

Pyright, in turn, relies on type inference and type hints to understand what autocompletion options you have available. While this does include support for comments, there is no support for type hints in docstrings (this was explicitly rejected in the type hinting spec).

If you are used to Typescript, Python type hints and Pylance should work in pretty much the same way. Your own example already includes type hints, and the literal translation of your example would be to use literal types:

from typing import Literal

def some_method(param1: Literal["option1", "option2"]) -> None:
    """A special method."""
    print(param1)```

You can then auto-complete on those values:

Visual Studio screenshot with Intellisense autocompletion offering 'option1' and 'option2' strings.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Nice! Shame it is buggy but hopefully, they give it some love soon. I found opening the parens then hitting `Ctrl + space` which shows the suggestions, then tab on whichever. The problem being it also inserts "the quotes" so if you haven't typed them then it inserts as expected, but without typing them you need to manually trigger the autocomplete suggestions. I would love to see this become native to vscode so I don't have to encourage people to install plugins to make the package easier. Thanks for the detailed answer! – Steve Feb 16 '21 at 00:16
  • 1
    @Steve: MS has triaged my report and marked it as a bug, so hopefully it'll get be fixed in the next version. – Martijn Pieters Feb 16 '21 at 21:08
  • Is it possible to trigger completion suggestion automatically without hitting `Ctrl Space`, like in TypeScript? – Yue JIN Nov 03 '22 at 09:13
  • @YueJIN: I don't know what you mean, exactly. It's up to Visual Studio Code and AI code-writing extensions like Copilot to make suggestions, based on the options the language server (like Pylance or the Typescript language server). Pylance offers completion options the same way the Typescript language server does. In my current setup, I see greyed out suggestions both for Pylance and Typescript, because I have Copilot enabled. Is that what you mean? – Martijn Pieters Nov 25 '22 at 14:29