python: flake8 找不到不存在的方法

python: flake8 cannot find non exising method

如果我这样做:

new_list = []
new_list.foo()

flake8 不会 return foo() 方法的错误,因为它不是 'list' 方法。

这是正常现象还是我需要配置什么?

flake8 不处理问题,但 PyLint 处理 - 发出 no-member 警告:

$ pylint test.py
No config file found, using default configuration
************* Module test
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "new_list" (invalid-name)
E:  2, 0: Instance of 'list' has no 'foo' member (no-member)

而且,built-in to PyCharm code analyzer 也会警告未解析的属性:

Flake8 is a wrapper around these tools:

  • PyFlakes
  • pep8
  • Ned Batchelder’s McCabe script

PyFlakes 是您可能希望检测到此类错误的部分。但是它检测的很少,它解释了为什么:

Pyflakes is also faster than Pylint or Pychecker. This is largely because Pyflakes only examines the syntax tree of each file individually. As a consequence, Pyflakes is more limited in the types of things it can check.

flake8 文档列出了 Pyflakes 提供的错误代码:

code    sample message
F401    module imported but unused
F402    import module from line N shadowed by loop variable
F403    ‘from module import *’ used; unable to detect undefined names
F404    future import(s) name after other statements
F811    redefinition of unused name from line N
F812    list comprehension redefines name from line N
F821    undefined name name
F822    undefined name name in __all__
F823    local variable name ... referenced before assignment
F831    duplicate argument name in function definition
F841    local variable name is assigned to but never used

我支持 PyCharm 的建议,其次是 PyLint。