Skip to content

Commit c1547d9

Browse files
authored
Merge pull request #21 from stdedos/replay/fix/nate/better-unused-argument
See more details in #21
2 parents 106c3b8 + bb6af8d commit c1547d9

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

pylint_pytest/checkers/fixture.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ def visit_module(self, node):
116116
is_test_module = True
117117
break
118118

119+
stdout, stderr = sys.stdout, sys.stderr
119120
try:
120121
with open(os.devnull, "w") as devnull:
121122
# suppress any future output from pytest
122-
stdout, stderr = sys.stdout, sys.stderr
123123
sys.stderr = sys.stdout = devnull
124124

125125
# run pytest session with customized plugin to collect fixtures
@@ -210,7 +210,7 @@ def visit_functiondef(self, node):
210210
for arg in node.args.args:
211211
self._invoked_with_func_args.add(arg.name)
212212

213-
# pylint: disable=bad-staticmethod-argument
213+
# pylint: disable=bad-staticmethod-argument,too-many-branches # The function itself is an if-return logic.
214214
@staticmethod
215215
def patch_add_message(
216216
self, msgid, line=None, node=None, args=None, confidence=None, col_offset=None
@@ -267,9 +267,18 @@ def patch_add_message(
267267
msgid == "unused-argument"
268268
and _can_use_fixture(node.parent.parent)
269269
and isinstance(node.parent, astroid.Arguments)
270-
and node.name in FixtureChecker._pytest_fixtures
271270
):
272-
return
271+
if node.name in FixtureChecker._pytest_fixtures:
272+
# argument is used as a fixture
273+
return
274+
275+
fixnames = (
276+
arg.name for arg in node.parent.args if arg.name in FixtureChecker._pytest_fixtures
277+
)
278+
for fixname in fixnames:
279+
if node.name in FixtureChecker._pytest_fixtures[fixname][0].argnames:
280+
# argument is used by a fixture
281+
return
273282

274283
# check W0621 redefined-outer-name
275284
if (
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
22+
@pytest.mark.parametrize("narg", [4, 5, 6])
23+
def test_nyfix(narg): # unused-argument
24+
"""A test function that does not use its param"""
25+
assert True
26+
27+
28+
@pytest.mark.parametrize("arg", [1, 2, 3])
29+
def test_narg_is_used_nowhere(myfix, narg):
30+
"""A test function that uses the param through a fixture"""
31+
assert myfix

tests/test_unused_argument.py

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

0 commit comments

Comments
 (0)