我怎样才能让 pylint 相信我的列表是可迭代的?

Bet*_*ker 5 python pylint python-2.7

我有一个 Python 应用程序,由于第三方库,我必须使用 Python 2.7,但我正在努力充分利用它。

我已经下载了 PyLint 1.9.5 并且基本上可以让它工作了。我遇到了一件事,但找不到解决方案。

我有一些课程基本上是这样的:

class Narg(object):
   [snip]

class Foo(object):
   def __init__(self):
      self.bar = None   # type: Optional[list[Narg]]

   def find(self):
      if self.bar:
         for narg in self.bar:
            [snip]
Run Code Online (Sandbox Code Playgroud)

pylint 不喜欢这个,它给了我

E:201,80: Non-iterable value self.bar is used in an iterating context (not-an-iterable)
Run Code Online (Sandbox Code Playgroud)

代码运行良好,所以它实际上很好,我怎样才能说服 pylint 呢?

mat*_*tch 5

pylint无法知道self.bar稍后可能会从存在更新None为可迭代。因此你有 2 个选择:

  1. 实例self.bar化为空可迭代 - 例如self.bar = []
  2. 禁用 pylint:self.bar = None # pylint: disable=not-an-iterable