如何处理 PyCharm 的“预期类型 X,得到 Y"

编程入门 行业动态 更新时间:2024-10-17 22:18: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.

你可以这样做:

  • 转到设置/首选项

    在侧边栏,点击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"

    本文发布于:2023-10-18 11:55:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1504161.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何处理   类型   PyCharm   quot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!