Skip to content

Commit

Permalink
Merge branch 'master' into add-python-3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Nov 11, 2023
2 parents e0526a8 + 3cee43d commit 425af3c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 10 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Slugify GITHUB_REPOSITORY
- name: Slugify GITHUB_REPOSITORY (win)
if: ${{ matrix.os == 'windows-latest' }}
run: |
$slug = $env:GITHUB_REPOSITORY -replace '/', '_'
echo "GITHUB_REPOSITORY_SLUG=$slug" | tee -Append $env:GITHUB_ENV
- name: Slugify GITHUB_REPOSITORY (non-win)
if: ${{ matrix.os != 'windows-latest' }}
run: echo "GITHUB_REPOSITORY_SLUG=${GITHUB_REPOSITORY////_}" >> $GITHUB_ENV

- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -51,7 +58,7 @@ jobs:
- name: Test with tox
env:
FORCE_COLOR: 1
PYTEST_CI_ARGS: --cov-report=xml --cov-report=html --junitxml=test_artifacts/test_report.xml --color=yes
PYTEST_CI_ARGS: --cov-report=xml --junitxml=test_artifacts/test_report.xml --color=yes
run: |
TOX_ENV=$(echo "py${{ matrix.python-version }}" | tr -d .)
tox -e $TOX_ENV
Expand Down
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@

- Support for Python 3.6 & 3.7 (#23)

## [1.1.4] - 2023-11-06

This is a small bugfix release.

### Fixed

* `anis-campos/fix_is_pytest_fixture`: (https://github.com/pylint-dev/pylint-pytest/pull/2)
Astroid has different semantics when using `import pytest` or `from pytest import ...`,
which affects the detection of pytest fixtures.

### Improved

* `pre-commit`: (https://github.com/pylint-dev/pylint-pytest/pull/20)
* Added more checks to the `pre-commit` hook.
```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
hooks:
- id: check-yaml
- id: check-toml
- id: check-vcs-permalinks
- id: check-shebang-scripts-are-executable
- id: name-tests-test
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
- id: yamlfmt
- repo: local
hooks:
- id: python-no-log-fatal
name: avoid logger.fatal(
```
* Unified formatting (always expanded arrays; not covered by linters, sadly)
## [1.1.3] - 2023-10-23
This is the first release after maintenance was assumed by https://github.com/stdedos.
Expand Down
17 changes: 13 additions & 4 deletions pylint_pytest/checkers/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,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 @@ -208,7 +208,7 @@ def visit_functiondef(self, node):
for arg in node.args.args:
self._invoked_with_func_args.add(arg.name)

# pylint: disable=bad-staticmethod-argument
# pylint: disable=bad-staticmethod-argument,too-many-branches # The function itself is an if-return logic.
@staticmethod
def patch_add_message(
self, msgid, line=None, node=None, args=None, confidence=None, col_offset=None
Expand Down Expand Up @@ -265,9 +265,18 @@ def patch_add_message(
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 node.name in FixtureChecker._pytest_fixtures:
# argument is used as a fixture
return

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:
# argument is used by a fixture
return

# check W0621 redefined-outer-name
if (
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module = [
check_untyped_defs = true

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

[tool.ruff]
# ruff is less lenient than pylint and does not make any exceptions
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="pylint-pytest",
version="1.1.3",
version="1.1.4",
author="Stavros Ntentos",
author_email="[email protected]",
license="MIT",
Expand Down
11 changes: 10 additions & 1 deletion tests/base_tester.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
from abc import ABC
from pathlib import Path
from pprint import pprint
from typing import Any, Dict, List

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


def get_test_root_path() -> Path:
"""Assumes ``base_tester.py`` is at ``<root>/tests``."""
return Path(__file__).parent


class BasePytestTester(ABC):
CHECKER_CLASS = BaseChecker
IMPACTED_CHECKER_CLASSES: List[BaseChecker] = []
Expand All @@ -41,7 +47,10 @@ def run_linter(self, enable_plugin):
# pylint: disable-next=protected-access
target_test_file = sys._getframe(1).f_code.co_name.replace("test_", "", 1)
file_path = os.path.join(
os.getcwd(), "tests", "input", self.MSG_ID, target_test_file + ".py"
get_test_root_path(),
"input",
self.MSG_ID,
target_test_file + ".py",
)

with open(file_path) as fin:
Expand Down
7 changes: 6 additions & 1 deletion tests/base_tester_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import pytest
from base_tester import BasePytestTester
from base_tester import BasePytestTester, get_test_root_path

# pylint: disable=unused-variable


def test_get_test_root_path():
assert get_test_root_path().name == "tests"
assert (get_test_root_path() / "input").is_dir()


def test_init_subclass_valid_msg_id():
some_string = "some_string"

Expand Down
34 changes: 34 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,34 @@
"""
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


@pytest.mark.parametrize("arg", [1, 2, 3])
def test_narg_is_used_nowhere(myfix, narg):
"""
A test function that does not use its param (``narg``):
Not itself, nor through a fixture (``myfix``).
"""
assert myfix
5 changes: 5 additions & 0 deletions tests/test_unused_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,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(2 if enable_plugin else 3)

0 comments on commit 425af3c

Please sign in to comment.