隐藏一些可能没有成员的Pylint错误

Liv*_*viu 15 python pylint python-3.4

以下Python片段代码通过以下方式分析Pylint:

if type(result) is array.array:
    read = result.tobytes()
Run Code Online (Sandbox Code Playgroud)

...最后一行出现以下错误:

E:401,22: Instance of 'int' has no 'tobytes' member\ 
 (but some types could not be inferred) (maybe-no-member)
Run Code Online (Sandbox Code Playgroud)

result变量是从外部函数接收.如何更改(更正)代码以使Pylint理解?或者我怎么能告诉它函数的结果可以有除int之外的其他类型?或者我怎么能告诉它忽略那条特定的线?(我赞成按此顺序回答问题)

sth*_*ult 27

由于某种原因,pylint没有得到'结果'可能是数组类型(并且肯定会在'if'分支下).目前没有办法告诉pylint,尽管希望在某些时候可能.因此,目前,您只能通过# pylint: disable=maybe-no-member在违规语句之后或在其上方添加来禁用该特定行的警告.例如:

if type(result) is array.array:
    read = result.tobytes() # pylint: disable=maybe-no-member
Run Code Online (Sandbox Code Playgroud)

要么

if type(result) is array.array:
    # pylint: disable=maybe-no-member
    read = result.tobytes()
Run Code Online (Sandbox Code Playgroud)

  • 上面评论中的链接已失效。[此](http://pylint.pycqa.org/en/latest/faq.html#is-it-possible-to-locally-disable-a-pspecial-message)是更新的链接。 (2认同)