pythonvisual-studio-codepylance

How does function arguments autocompletion work on Visual Studio Code?


I'm currently coding in python on Visual Studio Code, and using tkinter.

I am wondering about autocompletion for function arguments, e.g.:

screenshot

What is responsible for it? I can think of either tkinter, Visual Studio Code, or Pylance (which is mentioned in similar autocompletion windows).

I found this from autocompletion:

from typing extensions import Literal

# Ellipses stand for other annotated parameters
def pack_configure(self, ..., side: Literal['left', 'right', 'top', 'bottom'], ...):
    ...

I would like to enable autocompletion for some function arguments. Is autocompletion caused by Literal? If so, is it a good practice to use it in my functions?


Solution

  • I finally found the answer to my question.

    My question was not to complete a function's name but the value of arguments (see again here).

    I have done some research about Literal. Here is an example use:

    from typing_extensions import Literal #Important, otherwise Literal doesn't exist
    
    class Person:
        def __init__(self, **attributes):
            self.attributes = attributes
    
        def get(self, attribute: Literal['first_name', 'last_name', 'age']):
            return self.attributes.get(attribute)
        
    john_smith = Person(first_name='John', last_name='Smith', age=34)
    print( john_smith.get('age') )
    

    that works well:

    image