简体   繁体   中英

Autocompletion with type hinting in PyCharm for lists of specific type

This answer shows how to use type hinting in PyCharm for lists. But is it possible to hint PyCharm what kind of objects are present in the list? I know a list in python can have objects of different type. But just for the sake of autocompletion, I want this. Like suppose, if I have a list of numpy.ndarrays , can I hint that so that when I do obj_list[0]. , PyCharm gives me autocompletion for the numpy.ndarray ? Thanks

Pycharm does have some support for this, but not perfect for now (I'm using version 2018.2.4).

For most types, it works well (built-in types and some simple custom classes are tested):

# hint type in function document:

def func(a):
    """
    :param a:
    :type a list[str]
    :return:
    """
    a[0]   # will autocomplete 

# another way, use `typing` module
from typing import List
def func(a: List[str]):
    a[0]   # will autocomplete

Unfortunately, both approaches do not work for numpy types now. Pycharm will complain about cannot find reference ndarray in __init__.py .

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