简体   繁体   English

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

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

I just put together the following "minimum" repro case (minimum in quotes because I wanted to ensure pylint threw no other errors, warnings, hints, or suggestions - meaning there's a bit of boilerplate):我只是整理了以下“最小”重现案例(引号中的最小值,因为我想确保pylint没有抛出其他错误、警告、提示或建议——这意味着有一些样板文件):

pylint_error.py : pylint_error.py

"""
Docstring
"""

import numpy as np


def main():
    """
    Main entrypoint
    """
    test = np.array([1])
    print(test.shape[0])


if __name__ == "__main__":
    main()

When I run pylint on this code ( pylint pylint_error.py ) I get the following output:当我在此代码( 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)

It claims that test.shape is not subscriptable, even though it quite clearly is.它声称test.shape是不可下标的,尽管它很明显是。 When I run the code it works just fine:当我运行代码时,它工作得很好:

$> python pylint_error.py
1

So what's causing pylint to become confused, and how can I fix it?那么是什么导致pylint变得混乱,我该如何解决呢?

Some additional notes:一些附加说明:

  • If I declare test as np.arange(1) the error goes away如果我将 test 声明为np.arange(1)错误就会消失
  • If I declare test as np.zeros(1) , np.zeros((1)) , np.ones(1) , or np.ones((1)) the error does not go away如果我将 test 声明为np.zeros(1)np.zeros((1))np.ones(1)np.ones((1)) ,则错误不会go 消失
  • If I declare test as np.full((1), 1) the error goes away如果我将 test 声明为np.full((1), 1)错误就会消失
  • Specifying the type ( test: np.ndarray = np.array([1]) ) does not fix the error指定类型( test: np.ndarray = np.array([1])不能修复错误
  • Specifying a dtype ( np.array([1], dtype=np.uint8) ) does not fix the error指定dtype ( np.array([1], dtype=np.uint8) )不能修复错误
  • Taking a slice of test ( test[:].shape ) makes the error go away进行一段测试( test[:].shape )会使错误 go 消失

My first instinct says that the inconsistent behavior with various NumPY methods ( arange vs zeros vs full , etc) suggests it's just a bug in NumPY .我的第一直觉说,与各种NumPY方法( arange vs zeros vs full等)不一致的行为表明这只是NumPY中的一个错误。 However it's possible there's some underlying concept to NumPY that I'm misunderstanding.但是,我可能误解了NumPY的一些基本概念。 I'd like to be sure I'm not writing code with undefined behavior that's only working on accident.我想确定我不是在编写具有未定义行为的代码,这些行为只会在意外情况下起作用。

I don't have enough reputation to comment, but it looks like this is an open issue: https://github.com/PyCQA/pylint/issues/3139我没有足够的声誉发表评论,但看起来这是一个未解决的问题: https://github.com/PyCQA/pylint/issues/3139

Until the issue is resolved on their end, I would just change the line to在他们最终解决问题之前,我会将行更改为

    print(test.shape[0])  # pylint: disable=E1136  # pylint/issues/3139

to my pylintrc file.到我的pylintrc文件。

As of November 2019:截至 2019 年 11 月:

As mentioned by one of the users in the discussion on GitHub you could resolve the problem by downgrading both pylint and astroid , eg in requirements.txt正如GitHub讨论中的一位用户所提到的,您可以通过同时降级pylintastroid来解决问题,例如在requirements.txt

astroid>=2.0, <2.3
pylint>=2.3, <2.4

or或者

pip install astroid==2.2.5 & pip install pylint==2.3.1

This was finally fixed with the release of astroid 2.4.0 in May 2020.这最终在 2020 年 5 月发布的 astroid 2.4.0 中得到解决。

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

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

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