Nagabhushan S N
Nagabhushan S N

Reputation: 7267

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

Upvotes: 2

Views: 647

Answers (1)

halfelf
halfelf

Reputation: 10097

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.

Upvotes: 4

Related Questions