Skip to content

Commit 333be5c

Browse files
Merge branch 'master' into drop-eol-python-add-3.12
2 parents bdb3ff7 + c1547d9 commit 333be5c

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,38 @@
66

77
- Support for Python 3.6 & 3.7 (#23)
88

9+
## [1.1.4] - 2023-11-06
10+
11+
This is a small bugfix release.
12+
13+
### Fixed
14+
15+
* `anis-campos/fix_is_pytest_fixture`: (https://github.com/pylint-dev/pylint-pytest/pull/2)
16+
Astroid has different semantics when using `import pytest` or `from pytest import ...`,
17+
which affects the detection of pytest fixtures.
18+
19+
### Improved
20+
21+
* `pre-commit`: (https://github.com/pylint-dev/pylint-pytest/pull/20)
22+
* Added more checks to the `pre-commit` hook.
23+
```yaml
24+
repos:
25+
- repo: https://github.com/pre-commit/pre-commit-hooks
26+
hooks:
27+
- id: check-yaml
28+
- id: check-toml
29+
- id: check-vcs-permalinks
30+
- id: check-shebang-scripts-are-executable
31+
- id: name-tests-test
32+
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
33+
- id: yamlfmt
34+
- repo: local
35+
hooks:
36+
- id: python-no-log-fatal
37+
name: avoid logger.fatal(
38+
```
39+
* Unified formatting (always expanded arrays; not covered by linters, sadly)
40+
941
## [1.1.3] - 2023-10-23
1042
1143
This is the first release after maintenance was assumed by https://github.com/stdedos.

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 (

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name="pylint-pytest",
14-
version="1.1.3",
14+
version="1.1.4",
1515
author="Stavros Ntentos",
1616
author_email="[email protected]",
1717
license="MIT",
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)