如何处理PyCharm的“预期类型X,取而代之的是Y"

编程入门 行业动态 更新时间:2024-10-17 14:28:28
本文介绍了如何处理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:

  • 转到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"

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

    发布评论

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

    >www.elefans.com

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