pylint 中 python 3.x 的未定义变量警告

undefined-variable Warning for python 3.x in pylint

我正在编写一个 python 脚本,但我刚刚在 python 3.x 中遇到了有关 pylint 检查的问题:

class m(object):
    def check_infile(self):
        infile = None
        if not isinstance(infile, file):
            print("infile variable is not a file type.")

输出:

E: 56,38: Undefined variable 'file' (undefined-variable)

我试图通过添加 # pylint: disable=E0602, undefined-variable, E0603 来消除这个问题,但没有任何帮助。有什么建议吗?

在 python3.x 中,file 不是有效类型:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> file
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined

所以 pylint 就在这里,您可能不想将其静音。您可能想检查对象是否具有类似文件的方法:

if not getattr(infile, 'read', None):
    print('definitely not a file...')