Skip to content

Commit e59bda7

Browse files
zero323sobolevn
andauthored
Update path argument (#89)
* Update path argument Closes #88 * Add types-setuptools to dev-requirements.txt * Add ignores on incompatible conditional defs * Adjust reportinfo * Reformat collect.py * Resort items * Test against pytest 6.x and 7.x * Put install in the right task * Adjust pytest-version as requested * Use qualifiers within matrix * Update test.yml Co-authored-by: Nikita Sobolev <[email protected]>
1 parent b002006 commit e59bda7

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
17+
pytest-version: ["~=6.2", "==7.0.0rc1"] # TODO: enable `~=7.0`
1718

1819
steps:
1920
- uses: actions/checkout@v2
@@ -25,6 +26,8 @@ jobs:
2526
run: |
2627
pip install -U pip setuptools wheel
2728
pip install -e .
29+
# Force correct `pytest` version for different envs:
30+
pip install -U "pytest${{ matrix.pytest-version }}"
2831
- name: Run tests
2932
run: pytest
3033

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ black
22
isort
33
types-decorator
44
types-PyYAML
5+
types-setuptools
56
-e .

pytest_mypy_plugins/collect.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
2+
import pathlib
23
import platform
34
import sys
45
import tempfile
56
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional, Set
67

8+
import pkg_resources
79
import pytest
810
import yaml
911
from _pytest.config.argparsing import Parser
@@ -76,7 +78,9 @@ class YamlTestFile(pytest.File):
7678
def collect(self) -> Iterator["YamlTestItem"]:
7779
from pytest_mypy_plugins.item import YamlTestItem
7880

79-
parsed_file = yaml.load(stream=self.fspath.read_text("utf8"), Loader=SafeLineLoader)
81+
# To support both Pytest 6.x and 7.x
82+
path = getattr(self, "path", None) or getattr(self, "fspath")
83+
parsed_file = yaml.load(stream=path.read_text("utf8"), Loader=SafeLineLoader)
8084
if parsed_file is None:
8185
return
8286

@@ -138,10 +142,19 @@ def _eval_skip(self, skip_if: str) -> bool:
138142
return eval(skip_if, {"sys": sys, "os": os, "pytest": pytest, "platform": platform})
139143

140144

141-
def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]:
142-
if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")):
143-
return YamlTestFile.from_parent(parent, fspath=path)
144-
return None
145+
if pkg_resources.parse_version(pytest.__version__) >= pkg_resources.parse_version("7.0.0rc1"):
146+
147+
def pytest_collect_file(file_path: pathlib.Path, parent: Node) -> Optional[YamlTestFile]:
148+
if file_path.suffix in {".yaml", ".yml"} and file_path.name.startswith(("test-", "test_")):
149+
return YamlTestFile.from_parent(parent, path=file_path, fspath=None)
150+
return None
151+
152+
else:
153+
154+
def pytest_collect_file(path: LocalPath, parent: Node) -> Optional[YamlTestFile]: # type: ignore[misc]
155+
if path.ext in {".yaml", ".yml"} and path.basename.startswith(("test-", "test_")):
156+
return YamlTestFile.from_parent(parent, fspath=path)
157+
return None
145158

146159

147160
def pytest_addoption(parser: Parser) -> None:

pytest_mypy_plugins/item.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
import tempfile
66
from configparser import ConfigParser
77
from pathlib import Path
8-
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
8+
from typing import (
9+
TYPE_CHECKING,
10+
Any,
11+
Callable,
12+
Dict,
13+
List,
14+
Optional,
15+
Tuple,
16+
Union,
17+
no_type_check,
18+
)
919

1020
import py
1121
import pytest
@@ -346,8 +356,11 @@ def repr_failure(
346356
else:
347357
return super().repr_failure(excinfo, style="native")
348358

349-
def reportinfo(self) -> Tuple[Union[py.path.local, str], Optional[int], str]:
350-
return self.fspath, None, self.name
359+
@no_type_check
360+
def reportinfo(self) -> Tuple[Union[py.path.local, Path, str], Optional[int], str]:
361+
# To support both Pytest 6.x and 7.x
362+
path = getattr(self, "path", None) or getattr(self, "fspath")
363+
return path, None, self.name
351364

352365
def _collect_python_path(
353366
self,

0 commit comments

Comments
 (0)