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
1 change: 1 addition & 0 deletions dev-requirements.txt
Expand Up @@ -2,4 +2,5 @@ black
isort
types-decorator
types-PyYAML
types-setuptools
-e .
36 changes: 29 additions & 7 deletions pytest_mypy_plugins/collect.py
@@ -1,9 +1,20 @@
import os
import pathlib
import platform
import sys
import tempfile
from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Mapping, Optional, Set

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 +87,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 +151,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