Skip to content

Commit 425af3c

Browse files
Merge branch 'master' into add-python-3.12
2 parents e0526a8 + 3cee43d commit 425af3c

File tree

9 files changed

+111
-10
lines changed

9 files changed

+111
-10
lines changed

.github/workflows/run-tests.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ jobs:
3333
steps:
3434
- uses: actions/checkout@v3
3535

36-
- name: Slugify GITHUB_REPOSITORY
36+
- name: Slugify GITHUB_REPOSITORY (win)
37+
if: ${{ matrix.os == 'windows-latest' }}
38+
run: |
39+
$slug = $env:GITHUB_REPOSITORY -replace '/', '_'
40+
echo "GITHUB_REPOSITORY_SLUG=$slug" | tee -Append $env:GITHUB_ENV
41+
42+
- name: Slugify GITHUB_REPOSITORY (non-win)
43+
if: ${{ matrix.os != 'windows-latest' }}
3744
run: echo "GITHUB_REPOSITORY_SLUG=${GITHUB_REPOSITORY////_}" >> $GITHUB_ENV
3845

3946
- name: Set up Python ${{ matrix.python-version }}
@@ -51,7 +58,7 @@ jobs:
5158
- name: Test with tox
5259
env:
5360
FORCE_COLOR: 1
54-
PYTEST_CI_ARGS: --cov-report=xml --cov-report=html --junitxml=test_artifacts/test_report.xml --color=yes
61+
PYTEST_CI_ARGS: --cov-report=xml --junitxml=test_artifacts/test_report.xml --color=yes
5562
run: |
5663
TOX_ENV=$(echo "py${{ matrix.python-version }}" | tr -d .)
5764
tox -e $TOX_ENV

CHANGELOG.md

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

1212
- Support for Python 3.6 & 3.7 (#23)
1313

14+
## [1.1.4] - 2023-11-06
15+
16+
This is a small bugfix release.
17+
18+
### Fixed
19+
20+
* `anis-campos/fix_is_pytest_fixture`: (https://github.com/pylint-dev/pylint-pytest/pull/2)
21+
Astroid has different semantics when using `import pytest` or `from pytest import ...`,
22+
which affects the detection of pytest fixtures.
23+
24+
### Improved
25+
26+
* `pre-commit`: (https://github.com/pylint-dev/pylint-pytest/pull/20)
27+
* Added more checks to the `pre-commit` hook.
28+
```yaml
29+
repos:
30+
- repo: https://github.com/pre-commit/pre-commit-hooks
31+
hooks:
32+
- id: check-yaml
33+
- id: check-toml
34+
- id: check-vcs-permalinks
35+
- id: check-shebang-scripts-are-executable
36+
- id: name-tests-test
37+
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
38+
- id: yamlfmt
39+
- repo: local
40+
hooks:
41+
- id: python-no-log-fatal
42+
name: avoid logger.fatal(
43+
```
44+
* Unified formatting (always expanded arrays; not covered by linters, sadly)
45+
1446
## [1.1.3] - 2023-10-23
1547
1648
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
@@ -114,10 +114,10 @@ def visit_module(self, node):
114114
is_test_module = True
115115
break
116116

117+
stdout, stderr = sys.stdout, sys.stderr
117118
try:
118119
with open(os.devnull, "w") as devnull:
119120
# suppress any future output from pytest
120-
stdout, stderr = sys.stdout, sys.stderr
121121
sys.stderr = sys.stdout = devnull
122122

123123
# run pytest session with customized plugin to collect fixtures
@@ -208,7 +208,7 @@ def visit_functiondef(self, node):
208208
for arg in node.args.args:
209209
self._invoked_with_func_args.add(arg.name)
210210

211-
# pylint: disable=bad-staticmethod-argument
211+
# pylint: disable=bad-staticmethod-argument,too-many-branches # The function itself is an if-return logic.
212212
@staticmethod
213213
def patch_add_message(
214214
self, msgid, line=None, node=None, args=None, confidence=None, col_offset=None
@@ -265,9 +265,18 @@ def patch_add_message(
265265
msgid == "unused-argument"
266266
and _can_use_fixture(node.parent.parent)
267267
and isinstance(node.parent, astroid.Arguments)
268-
and node.name in FixtureChecker._pytest_fixtures
269268
):
270-
return
269+
if node.name in FixtureChecker._pytest_fixtures:
270+
# argument is used as a fixture
271+
return
272+
273+
fixnames = (
274+
arg.name for arg in node.parent.args if arg.name in FixtureChecker._pytest_fixtures
275+
)
276+
for fixname in fixnames:
277+
if node.name in FixtureChecker._pytest_fixtures[fixname][0].argnames:
278+
# argument is used by a fixture
279+
return
271280

272281
# check W0621 redefined-outer-name
273282
if (

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module = [
7070
check_untyped_defs = true
7171

7272
[tool.pytest.ini_options]
73-
addopts = "--verbose --cov-config=pyproject.toml"
73+
addopts = "--verbose --cov-config=pyproject.toml --cov-report=html"
7474

7575
[tool.ruff]
7676
# ruff is less lenient than pylint and does not make any exceptions

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",

tests/base_tester.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
from abc import ABC
4+
from pathlib import Path
45
from pprint import pprint
56
from typing import Any, Dict, List
67

@@ -21,6 +22,11 @@
2122
pylint_pytest.checkers.fixture.FILE_NAME_PATTERNS = ("*",)
2223

2324

25+
def get_test_root_path() -> Path:
26+
"""Assumes ``base_tester.py`` is at ``<root>/tests``."""
27+
return Path(__file__).parent
28+
29+
2430
class BasePytestTester(ABC):
2531
CHECKER_CLASS = BaseChecker
2632
IMPACTED_CHECKER_CLASSES: List[BaseChecker] = []
@@ -41,7 +47,10 @@ def run_linter(self, enable_plugin):
4147
# pylint: disable-next=protected-access
4248
target_test_file = sys._getframe(1).f_code.co_name.replace("test_", "", 1)
4349
file_path = os.path.join(
44-
os.getcwd(), "tests", "input", self.MSG_ID, target_test_file + ".py"
50+
get_test_root_path(),
51+
"input",
52+
self.MSG_ID,
53+
target_test_file + ".py",
4554
)
4655

4756
with open(file_path) as fin:

tests/base_tester_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import pytest
2-
from base_tester import BasePytestTester
2+
from base_tester import BasePytestTester, get_test_root_path
33

44
# pylint: disable=unused-variable
55

66

7+
def test_get_test_root_path():
8+
assert get_test_root_path().name == "tests"
9+
assert (get_test_root_path() / "input").is_dir()
10+
11+
712
def test_init_subclass_valid_msg_id():
813
some_string = "some_string"
914

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"""
31+
A test function that does not use its param (``narg``):
32+
Not itself, nor through a fixture (``myfix``).
33+
"""
34+
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)