2

I have recently been learning python and I was trying out some things in Pycharm when I noticed the following warning caused by this piece of code:

Class 'Iterable' does not define '_getitem_', so the operator '[]' cannot be used on its instances.

def test_list(var):
    """
    Parameters
    ----------
    var : list


    Returns
    -------
    int
    """
    return var[0]

I noticed that replacing 'list' by 'list[int]' in the code gets rid of the warning but using 'list of int' doesn't (which if I understand the numpydoc guide correctly is the proper way to do things). So what am I doing wrong exactly?

djvg
  • 11,722
  • 5
  • 72
  • 103
Demophilus
  • 129
  • 3
  • @PaulRooney I’m not passing anything, this is just a warning that the IDE pycharm gives me. The code runs fine, there are no errors, I was just wondering why pycharm gives me this warning and how to best solve it. – Demophilus Jan 10 '20 at 10:19
  • Also,i noticed if you put whitespace between list name and brackets containing index , then the warning vanishes. – gaurav1903 Aug 15 '20 at 21:34
  • https://docs.python.org/3/glossary.html#term-iterable – djvg Feb 14 '23 at 13:54

2 Answers2

-1

I have tried this and its working fine.

def funct1(var):
    return var[0]

print(funct1([1,2,3]))

OR

def funct1(var):
    return var[1]

print(funct1(["V","A","I"]))
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
  • 1
    Sorry, I may not have been clear enough. Pycharm is just giving a warning, not an error. The code will run fine, but the warning is bothering me. Removing the docstring all together would ofcourse also remove the warning, but that's not really the solution I'm looking for. – Demophilus Jan 10 '20 at 10:07
-1

Pycharm has type hinting features that may be of use.

This causes the warning. Pycharm Says,

PyCharm provides various means to assist inspecting and checking the types of the objects in your script. PyCharm supports type hinting in function annotations and type comments using the typing module and the format defined by PEP 484.

This seems only started from Python 3.5 onwards. This was a very discussed warning in pycharm community. Click here to get more idea about type hinting in pycharm from the community.