25

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
Vince W.
  • 3,561
  • 3
  • 31
  • 59
  • Why don't you just iterate over `ydata` directly? Also, note that you can use Sphinx markup to tell PyCharm what the parameters are: https://www.jetbrains.com/pycharm/help/creating-documentation-comments.html – jonrsharpe Mar 31 '15 at 16:51
  • 1
    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 accordingly – Vince W. Mar 31 '15 at 17:05
  • Bear in mind that one of your options is to just ignore it - it's only a warning, after all. – jonrsharpe Mar 31 '15 at 17:07
  • 1
    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. – dashesy Feb 01 '16 at 19:57

2 Answers2

11

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):
    ...
Vince W.
  • 3,561
  • 3
  • 31
  • 59
tzaman
  • 46,925
  • 11
  • 90
  • 115
2

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.

Nilesh Kevlani
  • 1,278
  • 1
  • 10
  • 10
  • 10
    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 editor – Vince W. Feb 06 '19 at 00:26
  • 1
    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. – ZaydH Oct 07 '20 at 19:05
  • Upvoted for redirecting future users to a better answer. – Nagabhushan S N Dec 07 '22 at 06:35