我只是将以下“最小”重现案例放在一起(最小引号是因为我想确保 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
)使错误消失 我的第一 react 是与各种不一致的行为
NumPY
方法( arange
vs zeros
vs full
等)表明它只是 NumPY
中的一个错误.然而,NumPY
可能存在一些潜在的概念。我误解了。我想确定我不是在编写具有未定义行为的代码,这些行为只会在偶然情况下起作用。
最佳答案
我没有足够的声誉来发表评论,但看起来这是一个悬而未决的问题:https://github.com/PyCQA/pylint/issues/3139
在他们最终解决问题之前,我只会将行更改为
print(test.shape[0]) # pylint: disable=E1136 # pylint/issues/3139
到我的
pylintrc
文件。
关于python - 为什么 pylint 为 numpy.ndarray.shape 返回 `unsubscriptable-object`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58646585/