PyCharm getitem warning for functions with arrays
Asked Answered
J

2

26

I'm getting code inspection warnings from PyCharm. I understand the logic, but I'm not clear on the appropriate way to fix it. Say I have the following example function:

def get_ydata(xdata):
    ydata = xdata ** 2
    for i in range(len(ydata)):
        print ydata[i]
return ydata

I get 2 warnings:

>> Expected type 'Sized', got 'int' instead (at line 3)
>> Class 'int' does not define '__getitem__', so the '[]' operator cannot be used on its instances (at line 4)

The purpose of the function is of course to parse a numpy array of xdata. But PyCharm doesn't know that, so without any further indication assumes that xdata (and therefore also ydata) is an integer.

What is the appropriate way to address this warning? I should note that adding a type checking line will fix the warning. Is that the optimal solution? For example:

if not type(ydata) is np.ndarray:
    ydata = np.array(ydata)

Lastly, adding Sphinx docstring information does not seem to have any effect on the warnings. (warning still sees 'int' when xdata is specified as str). Also iterating over y directly results in the following error:

for y in ydata:
...
>> Expected 'collections.Iterable', got 'int' instead
July answered 31/3, 2015 at 16:48 Comment(4)
Why don't you just iterate over ydata directly? Also, note that you can use Sphinx markup to tell PyCharm what the parameters are: jetbrains.com/pycharm/help/creating-documentation-comments.htmlThick
As for style, there reasons why I use an indexing based approach, rather than iterating over ydata directly. That aside, I just checked and the Sphinx-based docstrings don't seem to have any effect on the warning. For example, specifiying xdata as str, still results in the same 'expected int' error, so it seems its not being taken into account. Furthermore, iterating over ydata directly only results in the following error: "Expected 'collections.Iterable', got 'int' instead". I will update my question accordinglyJuly
Bear in mind that one of your options is to just ignore it - it's only a warning, after all.Thick
PyCharm is very smart, I also use a lot of array operations and these false positives are sometimes annoying because they hide real problems. I am sure if you create an issue they will fix it though, it already is much improved every day.Slue
C
12

Pycharm has type hinting features that may be of use.

For example in this case, the following code makes the errors go away:

import numpy as np

def get_ydata(xdata):
    ydata = xdata ** 2  # type: np.ndarray
    for i in range(len(ydata)):
        print(ydata[i])
    return ydata

Recent python versions also include support for type annotations

import numpy as np
def get_ydata(xdata: np.ndarray):
    ...
Calcine answered 31/3, 2015 at 17:14 Comment(0)
D
1

TL;DR Cast it using list()

Its late, still,

I had similar problem with some other code.

I could solve it by something similar to

def get_ydata(xdata):
    ydata = list(xdata ** 2)
    for i in range(len(ydata)):
        print ydata[i]
    return ydata

Consider the accepted answer. Comments on my answer are valid.

Dyscrasia answered 31/1, 2019 at 6:23 Comment(3)
Unless there is a reason to cast the array to a list, type annotations are the correct solution. We should not be copying memory and creating new data objects just to silence the editorJuly
To follow-up on Vince's comment, you are better off using one of PyCharm's built-in commands (e.g. # noqa although that may be overly broad) to hide the error (which has no runtime error). At least then someone reading the code can see that a deliberate decision reading a linter error was made.Campfire
Upvoted for redirecting future users to a better answer.Claireclairobscure

© 2022 - 2024 — McMap. All rights reserved.