diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a50a13f..934704b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,7 @@ jobs: strategy: matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] + pytest-version: ["~=6.2", "==7.0.0rc1"] # TODO: enable `~=7.0` steps: - uses: actions/checkout@v2 @@ -25,6 +26,8 @@ jobs: run: | pip install -U pip setuptools wheel pip install -e . + # Force correct `pytest` version for different envs: + pip install -U "pytest${{ matrix.pytest-version }}" - name: Run tests run: pytest diff --git a/dev-requirements.txt b/dev-requirements.txt index 6121c8d..9ef9dde 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -2,4 +2,5 @@ black isort types-decorator types-PyYAML +types-setuptools -e . diff --git a/pytest_mypy_plugins/collect.py b/pytest_mypy_plugins/collect.py index d1ebd86..a8ad8a0 100644 --- a/pytest_mypy_plugins/collect.py +++ b/pytest_mypy_plugins/collect.py @@ -1,9 +1,11 @@ import os +import pathlib import platform import sys import tempfile from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional, Set +import pkg_resources import pytest import yaml from _pytest.config.argparsing import Parser @@ -76,7 +78,9 @@ class YamlTestFile(pytest.File): def collect(self) -> Iterator["YamlTestItem"]: from pytest_mypy_plugins.item import YamlTestItem - parsed_file = yaml.load(stream=self.fspath.read_text("utf8"), Loader=SafeLineLoader) + # To support both Pytest 6.x and 7.x + path = getattr(self, "path", None) or getattr(self, "fspath") + parsed_file = yaml.load(stream=path.read_text("utf8"), Loader=SafeLineLoader) if parsed_file is None: return @@ -138,10 +142,19 @@ def _eval_skip(self, skip_if: str) -> bool: return eval(skip_if, {"sys": sys, "os": os, "pytest": pytest, "platform": platform}) -def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]: - if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")): - return YamlTestFile.from_parent(parent, fspath=path) - return None +if pkg_resources.parse_version(pytest.__version__) >= pkg_resources.parse_version("7.0.0rc1"): + + def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> Optional[YamlTestFile]: + if file_path.suffix in {".yaml", ".yml"} and file_path.name.startswith(("test-", "test_")): + return YamlTestFile.from_parent(parent, path=file_path, fspath=None) + return None + +else: + + def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]: # type: ignore[misc] + if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")): + return YamlTestFile.from_parent(parent, fspath=path) + return None def pytest_addoption(parser: Parser) -> None: diff --git a/pytest_mypy_plugins/item.py b/pytest_mypy_plugins/item.py index 3662c13..8775567 100644 --- a/pytest_mypy_plugins/item.py +++ b/pytest_mypy_plugins/item.py @@ -5,7 +5,17 @@ import tempfile from configparser import ConfigParser from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Dict, + List, + Optional, + Tuple, + Union, + no_type_check, +) import py import pytest @@ -346,8 +356,11 @@ def repr_failure( else: return super().repr_failure(excinfo, style="native") - def reportinfo(self) -> Tuple[Union[py.path.local, str], Optional[int], str]: - return self.fspath, None, self.name + @no_type_check + def reportinfo(self) -> Tuple[Union[py.path.local, Path, str], Optional[int], str]: + # To support both Pytest 6.x and 7.x + path = getattr(self, "path", None) or getattr(self, "fspath") + return path, None, self.name def _collect_python_path( self,