Skip to content

Commit

Permalink
Update path argument (#89)
Browse files Browse the repository at this point in the history
* 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 <mail@sobolevn.me>
  • Loading branch information
zero323 and sobolevn committed Dec 21, 2021
1 parent b002006 commit e59bda7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Expand Up @@ -2,4 +2,5 @@ black
isort
types-decorator
types-PyYAML
types-setuptools
-e .
23 changes: 18 additions & 5 deletions 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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
19 changes: 16 additions & 3 deletions pytest_mypy_plugins/item.py
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e59bda7

Please sign in to comment.