简体   繁体   中英

Python seeing function parameters as class for autocompletion

Is it possible to declare python functions with specific class parameters? The reason is that I want to avoid type errors (Happens to me quiet often)

For example:

def connect(socket as socket, HOST, PORT):
    return socket.connect((HOST, PORT))

The goal here would be that if I type 'socket.' the autocompletion would show me any functions available on the socket class. Since I am passing it as an argument the autocompletion which pops up is empty. The reason is that you can pass every object to the function.

Python has syntactic support for type annotations, but Python itself doesn't have autocomplete. That's provided by third-party tools, like PyCharm and Jedi. Some of these applications (like PyCharm) can use static inference to get better completions if you annotate your functions properly.

Say you start typing in a function into PyCharm like this,

def foo(a_dict: dict) -> int:
    print(a_dict.i▏

Then it will be able to infer that a_dict is a dict and pop up a completion window with dict methods, like items() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM