I have a fixture that creates a mock object, patches a library to return that mock object for a specific method and then returns the mock object so that assertions can be made on it. It seems quite clear that I am in fact using the variable, not sure why it's triggering the linting rule.
I was able to refactor the code to make the lint pass (the second example below), but that code is much less intuitive to me and messy.
What I Did
@pytest.fixture()
def example_mock_fails_linting(mocker):
mock = mocker.MagicMock()
mocker.patch("path.to.mocked.method.that.returns.an.object").return_value = mock
return mock # R504 triggers for this line
@pytest.fixture()
def example_mock_lints_clean(mocker):
example = mocker.patch("path.to.mocked.method.that.returns.an.object")
example.return_value = mocker.MagicMock()
return example.return_value # R504 doesn't trigger for this line
Description
I have a fixture that creates a mock object, patches a library to return that mock object for a specific method and then returns the mock object so that assertions can be made on it. It seems quite clear that I am in fact using the variable, not sure why it's triggering the linting rule.
I was able to refactor the code to make the lint pass (the second example below), but that code is much less intuitive to me and messy.
What I Did