我只整理了以下“最小”复制案例(最小引号,因为我想确保pylint没有抛出其他错误,警告,提示或建议-这意味着有些重复):
pylint_error.py:
"""
Docstring
"""
import numpy as np
def main():
"""
Main entrypoint
"""
test = np.array([1])
print(test.shape[0])
if __name__ == "__main__":
main()
当我pylint在此代码(pylint pylint_error.py)上运行时,得到以下输出:
$> pylint pylint_error.py
************* Module pylint_error
pylint_error.py:13:10: E1136: Value 'test.shape' is unsubscriptable (unsubscriptable-object)
------------------------------------------------------------------
Your code has been rated at 1.67/10 (previous run: 1.67/10, +0.00)
它声称这test.shape是不可下标的,即使很明显也是如此。当我运行代码时,它可以正常工作:
$> python pylint_error.py
1
那么,是什么导致pylint变得困惑,我该如何解决?
一些附加说明:
- 如果我声明测试
np.arange(1)错误消失 - 如果我宣布试验为
np.zeros(1),np.zeros((1)),np.ones(1),或np.ones((1))错误也不会消失 - 如果我声明测试
np.full((1), 1)错误消失 - 指定类型(
test: np.ndarray = np.array([1]))并没有修复错误 - 指定
dtype(np.array([1], dtype=np.uint8))并没有修复错误 - 进行测试(
test[:].shape)可消除错误
我的第一个直觉是,各种NumPY方法(arangevs,zerosvs full等)不一致的行为表明,这只是in的一个错误NumPY。但是,可能NumPY我误会了一些基本概念。我想确保我不会编写具有意外行为的未定义行为的代码。
pylint前numpy