Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

removes more false positives for unused-argument #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions pylint_pytest/checkers/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ def visit_module(self, node):
is_test_module = True
break

stdout, stderr = sys.stdout, sys.stderr
try:
with open(os.devnull, 'w') as devnull:
# suppress any future output from pytest
stdout, stderr = sys.stdout, sys.stderr
sys.stderr = sys.stdout = devnull

# run pytest session with customized plugin to collect fixtures
Expand Down Expand Up @@ -238,11 +238,16 @@ def patch_add_message(self, msgid, line=None, node=None, args=None,
return

# check W0613 unused-argument
if msgid == 'unused-argument' and \
_can_use_fixture(node.parent.parent) and \
isinstance(node.parent, astroid.Arguments) and \
node.name in FixtureChecker._pytest_fixtures:
return
if msgid == 'unused-argument':
if _can_use_fixture(node.parent.parent):
if isinstance(node.parent, astroid.Arguments):
if node.name in FixtureChecker._pytest_fixtures:
return # argument is used as a fixture
else:
fixnames = (arg.name for arg in node.parent.args if arg.name in FixtureChecker._pytest_fixtures)
for fixname in fixnames:
if node.name in FixtureChecker._pytest_fixtures[fixname][0].argnames:
return # argument is used by a fixture

# check W0621 redefined-outer-name
if msgid == 'redefined-outer-name' and \
Expand Down
24 changes: 24 additions & 0 deletions tests/input/unused-argument/func_param_as_fixture_arg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
This module illustrates a situation in which unused-argument should be
suppressed, but is not.
"""

import pytest


@pytest.fixture
def myfix(arg):
"""A fixture that requests a function param"""
print("arg is ", arg)
return True


@pytest.mark.parametrize("arg", [1, 2, 3])
def test_myfix(myfix, arg):
"""A test function that uses the param through a fixture"""
assert myfix

@pytest.mark.parametrize("narg", [4, 5, 6])
def test_nyfix(narg): # unused-argument
"""A test function that does not use its param"""
assert True
5 changes: 5 additions & 0 deletions tests/test_unused_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ def test_caller_not_a_test_func(self, enable_plugin):
def test_args_and_kwargs(self, enable_plugin):
self.run_linter(enable_plugin)
self.verify_messages(2)

@pytest.mark.parametrize('enable_plugin', [True, False])
def test_func_param_as_fixture_arg(self, enable_plugin):
self.run_linter(enable_plugin)
self.verify_messages(1 if enable_plugin else 2)