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

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

问题描述

使用 PyCharm 时,如果我编写 np.array,Pycharm 的代码样式检查会给我警告 Expected type 'Union[ndarray, Iterable]', got 'float' instead 在编辑器中(0.0).当我写 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)

我得到 预期的类型 'ndarray',得到的是 'float',而

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. However, please be warned,

完全关闭检查不是一个好的解决方案.最多PyCharm 做对了,这提供了有用的反馈.如果弄错了,最好跟他们提票看看如果可以解决的话.

Switching off the inspection completely is not a good solution. Most of the time PyCharm gets it right and this provides useful feedback. If it's getting it wrong, it's best to raise a ticket with them to see if it can be fixed.

你可以这样做:

  1. 转到设置/首选项

在侧边栏,点击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天全站免登陆