Search code examples
pythonpython-3.xmockingpytestpylint

How to get pylint to recognise method members of mocks?


Test example:

import pytest
def test_do_stuff(mocker):
    import my_module
    mocker.patch.object(my_module, 'do_stuff')
    my_module.do_stuff.return_value = True

Pylint feedback:

E1101: Function 'do_stuff' has no 'return_value' member (no-member)

This is incorrect as my_module.do_stuff() has been replaced with a mock, which supports this call, however, it seems that Pylint does not understand this.

I could disable the no-member warning type entirely with # pylint: disable=E1101 at the top of the test file, but this would also hide legitimate warnings (e.g. an incorrect function call.)

Is there a way to make Pylint work with mock objects?


Solution

  • As far as I'm aware, there's no way to achieve this automatically.

    Placing the Pylint disable comment on the end of the line raising the error will suppress the warning only on that line and won't stop analysis of the rest of the file, but the comment will need adding to each problematic line.