python - 为什么 pylint 为 numpy.ndarray.shape 返回 `unsubscriptable-object`?

标签 python numpy pylint

我只是将以下“最小”重现案例放在一起(最小引号是因为我想确保 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/

    相关文章:

    python - Pylint 给出错误,但代码工作正常

    python - pylint 中生成成员的特定类?

    python - 如何获取元组列表并将元组从字符串更改为整数

    python - 使用 numpy.logspace() 时出错 : how to generate numbers spaced evenly on a log-scale

    python - 有没有一种简单的方法来交换矩阵中的行以在左侧形成单位矩阵

    python - 为什么要使用 tf.data?

    python - 用链式掩码替换 numpy 数组元素

    python - .Pytest session 范围固定装置显示 pylint 中的错误

    python - 在 PIL 中制作拼贴画

    python - 如何添加 <div> 标签而不是 <li>