How to deal with PyCharm's "Expected type X, got Y instead"

Viewed 51030

When using PyCharm, Pycharm's code style inspection gives me the warning Expected type 'Union[ndarray, Iterable]', got 'float' instead in the editor if I write np.array(0.0). When I write np.array([0.0]) I get no warning.

When coding

from scipy.special import expit
expit(0.0)

I get Expected type 'ndarray', got 'float' instead, while

expit(np.array([0.0]))

solves that.

What I think Pycharm's code style inspection wants to tell me is there's a possibility of a type error, but I am not sure how I should react to that in the sense of good programming. Is PyCharm right to scold me and should I use the long versions or should I keep my short versions for readability and speed of coding?

If I should not change my code to the long versions - can I get rid of the Pycharm's code style inspection warning, or is that a bad idea, because they may be correct in other cases, and I am not able to tune the warnings that specifically?

4 Answers

If you want to ignore it for that specific line,

then you can put

# noinspection PyTypeChecker

above the line of code that is being marked.

As @brunodd said on a comment before, the correct tag to uncheck is Type checker.

  • Go to Settings/Preferences (Ctrl + Alt + S)
  • On the sidebar Inspections
  • Python tab
  • Uncheck Type Checker and hit Apply

This is really annoying, PyCharm fails to identify some types even though its right.

Related