Hide some maybe-no-member Pylint errors

纵饮孤独 提交于 2019-12-12 09:31:08

问题


The following Python fragment code gets analyzed by Pylint:

if type(result) is array.array:
    read = result.tobytes()

... with the following error for the last line:

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

The result variable is received from an external function. How can I change (correct) the code to make Pylint understand? Or how can I tell it that the result of the function can have other types than int? Or how can I tell it to ignore that particular line? (I favor an answer in this order of the questions)


回答1:


For some reason, pylint doesn't get 'result' may be of the array type (and will be for sure under the 'if' branch). There is currently no way to tell pylint about that, though it will hopefully be possible at some point. So for now, you can only disable the warning for that specific line by adding # pylint: disable=maybe-no-member after the offending statement or right above it. For example:

if type(result) is array.array:
    read = result.tobytes() # pylint: disable=maybe-no-member

or

if type(result) is array.array:
    # pylint: disable=maybe-no-member
    read = result.tobytes()



回答2:


I disabled all no-member warnings by passing this command line option to pylint

--disable=E1101



回答3:


Instead of

result.tobytes(), 

use

getattr(result, 'tobytes')()


来源:https://stackoverflow.com/questions/26657265/hide-some-maybe-no-member-pylint-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!