Skip to content

Commit bb0b523

Browse files
committed
removes more false positives for unused-argument
1 parent 6267638 commit bb0b523

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

pylint_pytest/checkers/fixture.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def visit_module(self, node):
110110
is_test_module = True
111111
break
112112

113+
stdout, stderr = sys.stdout, sys.stderr
113114
try:
114115
with open(os.devnull, 'w') as devnull:
115116
# suppress any future output from pytest
116-
stdout, stderr = sys.stdout, sys.stderr
117117
sys.stderr = sys.stdout = devnull
118118

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

240240
# check W0613 unused-argument
241-
if msgid == 'unused-argument' and \
242-
_can_use_fixture(node.parent.parent) and \
243-
isinstance(node.parent, astroid.Arguments) and \
244-
node.name in FixtureChecker._pytest_fixtures:
245-
return
241+
if msgid == 'unused-argument':
242+
if _can_use_fixture(node.parent.parent):
243+
if isinstance(node.parent, astroid.Arguments):
244+
if node.name in FixtureChecker._pytest_fixtures:
245+
return # argument is used as a fixture
246+
else:
247+
fixnames = (arg.name for arg in node.parent.args if arg.name in FixtureChecker._pytest_fixtures)
248+
for fixname in fixnames:
249+
if node.name in FixtureChecker._pytest_fixtures[fixname][0].argnames:
250+
return # argument is used by a fixture
246251

247252
# check W0621 redefined-outer-name
248253
if msgid == 'redefined-outer-name' and \
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
This module illustrates a situation in which unused-argument should be
3+
suppressed, but is not.
4+
"""
5+
6+
import pytest
7+
8+
9+
@pytest.fixture
10+
def myfix(arg):
11+
"""A fixture that requests a function param"""
12+
print("arg is ", arg)
13+
return True
14+
15+
16+
@pytest.mark.parametrize("arg", [1, 2, 3])
17+
def test_myfix(myfix, arg):
18+
"""A test function that uses the param through a fixture"""
19+
assert myfix
20+
21+
@pytest.mark.parametrize("narg", [4, 5, 6])
22+
def test_nyfix(narg): # unused-argument
23+
"""A test function that does not use its param"""
24+
assert True

tests/test_unused_argument.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ def test_caller_not_a_test_func(self, enable_plugin):
2828
def test_args_and_kwargs(self, enable_plugin):
2929
self.run_linter(enable_plugin)
3030
self.verify_messages(2)
31+
32+
@pytest.mark.parametrize('enable_plugin', [True, False])
33+
def test_func_param_as_fixture_arg(self, enable_plugin):
34+
self.run_linter(enable_plugin)
35+
self.verify_messages(1 if enable_plugin else 2)

0 commit comments

Comments
 (0)