繁体   English   中英

为什么 pylint 为 numpy.ndarray.shape 返回“unsubscriptable-object”?

[英]Why pylint returns `unsubscriptable-object` for numpy.ndarray.shape?

我只是整理了以下“最小”重现案例(引号中的最小值,因为我想确保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_error.py pylint上运行 pylint 时,我得到以下 output:

$> 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变得混乱,我该如何解决呢?

一些附加说明:

  • 如果我将 test 声明为np.arange(1)错误就会消失
  • 如果我将 test 声明为np.zeros(1)np.zeros((1))np.ones(1)np.ones((1)) ,则错误不会go 消失
  • 如果我将 test 声明为np.full((1), 1)错误就会消失
  • 指定类型( test: np.ndarray = np.array([1])不能修复错误
  • 指定dtype ( np.array([1], dtype=np.uint8) )不能修复错误
  • 进行一段测试( test[:].shape )会使错误 go 消失

我的第一直觉说,与各种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讨论中的一位用户所提到的,您可以通过同时降级pylintastroid来解决问题,例如在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 中得到解决。

https://github.com/PyCQA/pylint/issues/3139

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM