Hide some maybe-no-member Pylint errors

Question:

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)

Asked By: Liviu

||

Answers:

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()
Answered By: sthenault

Instead of

result.tobytes(), 

use

getattr(result, 'tobytes')()
Answered By: Brian

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

--disable=E1101
Answered By: Boris Verkhovskiy

While using PyLint you can generate a configuration pylint file using these command in your terminal

$ pylint –generate-rcfile > .pylintrc

the generated file handles the errors and warnings that you have to check.
for the no member in particular you can go to line 560, you can find that it ignored.
this configurations will be applied to all your code files.
and you can edit this configuration file, to meet your requirements.

Answered By: HadyKh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.