How to deal with PyCharm#39;s quot;Expected type X, got Y insteadquot;(如何处理 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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何处理 PyCharm 的“预期类型 X,得到 Y"
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
