gpt4 book ai didi

python - PyCharm 认为 int/float 没有实现 __div__ 的那一天

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:25 24 4
gpt4 key购买 nike

更新PyCharm 2017.2 解决了这个问题

def foo(x):
return x / (60 * 60)

z = 1
y = 2
bar = 0 if y == 0 else z / y

foo(bar)

这会导致具有几乎默认分析器设置(我相信)的 PyCharm 2017.1.4 显示以下警告:

Expected type '{__div__}', got 'Union[int, float]' instead.

This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases

现在,当然 Python 没有静态类型检查,但这里很明显 bar 将始终是 intfloat (并且 PyCharm 通过上述警告中的 got Union[int, float] 明白这一点)。

那么,这是 PyCharm 中的一个错误吗?在这个特定的用例中,它没有看到 intfloat 确实实现了 __div__ ,还是我还缺少其他东西?

更新

奇怪的是,如果 foo 被修改为 return x * 60 * 60 警告消失并且 PyCharm 不会警告 x未实现 __mul__

更新 2

foo 更改为 return float(x)/(60 * 60) 显然会使警告消失,但 x 已被推断为intfloat 由警告本身证明。

绝对像是 PyCharm 中的错误。

最佳答案

这好像是你的PyCharm版本的bug,我在2017.1.2无法重现

编辑:您使用哪个 Python 编译器? 2.7x 还是 3.x?

关于python - PyCharm 认为 int/float 没有实现 __div__ 的那一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44927363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com