如何处理PyCharm的“预期类型X,取而代之的是Y" [英] How to deal with PyCharm's "Expected type X, got Y instead"

查看:154
本文介绍了如何处理PyCharm的“预期类型X,取而代之的是Y"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PyCharm时,如果我编写np.array(0.0),Pycharm的代码样式检查将在编辑器中向我发出警告Expected type 'Union[ndarray, Iterable]', got 'float' instead.当我写np.array([0.0])时,我没有得到警告.

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.

编码时

from scipy.special import expit
expit(0.0)

我得到Expected type 'ndarray', got 'float' instead,而

expit(np.array([0.0]))

解决了这个问题.

我认为Pycharm的代码样式检查想告诉我,有可能出现类型错误,但是我不确定我应该如何从良好的编程意义上对此做出反应. PyCharm是否应该责骂我,我应该使用长版本还是应该保留我的短版本以提高可读性和编码速度?

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?

如果我不应该将代码更改为长版本-我可以摆脱Pycharm的代码样式检查警告,还是一个坏主意,因为在其他情况下它们可能是正确的,并且我无法调整具体的警告?

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?

推荐答案

PyCharm从源代码的类型提示中确定您传递的参数不正确.

PyCharm determines from the type-hints of the source code that the arguments you pass are incorrect.

您的问题简化为弄清楚如何禁用此类型检查的问题之一.您可以这样做:

Your question simplifies to one of figuring out how to disable this type checking. You can do that like this:

  1. 转到Settings/Preferences

在侧边栏上,单击Inspections

展开Python标签

向下滚动到Type Checker并取消选中它

Scroll down to Type Checker and uncheck it

PyCharm现在应该停止发出有关错误的函数参数的警告.

PyCharm should now stop issuing warnings about incorrect function arguments.

这篇关于如何处理PyCharm的“预期类型X,取而代之的是Y"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆