Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update path argument #89

Merged
merged 11 commits into from Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 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.5", "7.0.0rc1"]
zero323 marked this conversation as resolved.
Show resolved Hide resolved

steps:
- uses: actions/checkout@v2
Expand All @@ -24,6 +25,7 @@ jobs:
- name: Install dependencies
run: |
pip install -U pip setuptools wheel
pip install -U "pytest==${{ matrix.pytest-version }}"
pip install -e .
- 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