为什么 pylint returns `unsubscriptable-object` 用于 numpy.ndarray.shape?
Why pylint returns `unsubscriptable-object` for numpy.ndarray.shape?
我只是整理了以下 "minimum" 重现案例(引号最少,因为我想确保 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))
,错误会 not走开
- 如果我将测试声明为
np.full((1), 1)
,错误就会消失
- 指定类型 (
test: np.ndarray = np.array([1])
) 不会 修复错误
- 指定
dtype
(np.array([1], dtype=np.uint8)
) 不会 修复错误
- 进行一些测试 (
test[:].shape
) 让错误消失
我的第一直觉说各种 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
文件。
截至 2019 年 11 月:
正如一位用户在 GitHub 的讨论中提到的,您可以通过降级 both pylint 来解决问题和 astroid,例如在 requirements.txt
astroid>=2.0, <2.3
pylint>=2.3, <2.4
或
pip install astroid==2.2.5 & pip install pylint==2.3.1
2020 年 5 月发布的 astroid 2.4.0 最终解决了这个问题。
我只是整理了以下 "minimum" 重现案例(引号最少,因为我想确保 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))
,错误会 not走开 - 如果我将测试声明为
np.full((1), 1)
,错误就会消失 - 指定类型 (
test: np.ndarray = np.array([1])
) 不会 修复错误 - 指定
dtype
(np.array([1], dtype=np.uint8)
) 不会 修复错误 - 进行一些测试 (
test[:].shape
) 让错误消失
我的第一直觉说各种 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
文件。
截至 2019 年 11 月:
正如一位用户在 GitHub 的讨论中提到的,您可以通过降级 both pylint 来解决问题和 astroid,例如在 requirements.txt
astroid>=2.0, <2.3
pylint>=2.3, <2.4
或
pip install astroid==2.2.5 & pip install pylint==2.3.1
2020 年 5 月发布的 astroid 2.4.0 最终解决了这个问题。