afonasev / flake8-return

Flake8 plugin for return expressions checking.
MIT License
62 stars 69 forks source link

Possible False Positive for R504 #125

Open crimsonknave opened 1 year ago

crimsonknave commented 1 year ago

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

@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
afonasev commented 1 year ago

Thank you for your issue! If you can try to fix this, I'm willing to consider a merge request.