- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
更新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
将始终是 int
或 float
(并且 PyCharm 通过上述警告中的 got Union[int, float]
明白这一点)。
那么,这是 PyCharm 中的一个错误吗?在这个特定的用例中,它没有看到 int
和 float
确实实现了 __div__
,还是我还缺少其他东西?
更新
奇怪的是,如果 foo
被修改为 return x * 60 * 60
警告消失并且 PyCharm 不会警告 x
未实现 __mul__
。
更新 2
将 foo
更改为 return float(x)/(60 * 60)
显然会使警告消失,但 x
已被推断为int
或 float
由警告本身证明。
绝对像是 PyCharm 中的错误。
最佳答案
这好像是你的PyCharm版本的bug,我在2017.1.2无法重现
编辑:您使用哪个 Python 编译器? 2.7x 还是 3.x?
关于python - PyCharm 认为 int/float 没有实现 __div__ 的那一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44927363/
这个问题在这里已经有了答案: Error when trying to overload an operator "/" (2 个答案) 关闭 7 年前。 我有一个类,其函数 mul 和 div 实
为什么numpy会为下面的代码抛出异常? foo/3 工作正常。 import numpy as np class item(object): def __init__(self, val =
元组代表分数。我试图通过乘以倒数来除分数 class Test(): def __init__(self): self._x=(1,2) def __div__(sel
更新PyCharm 2017.2 解决了这个问题 def foo(x): return x / (60 * 60) z = 1 y = 2 bar = 0 if y == 0 else z /
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是一名优秀的程序员,十分优秀!