2
2
# SPDX-License-Identifier: Apache-2.0 OR MIT
3
3
4
4
import os
5
+ import pathlib
5
6
import tempfile
6
- from typing import Iterable , Iterator , List , NamedTuple , Optional , Tuple
7
+ from typing import Iterable , Iterator , List , NamedTuple , Optional , Tuple , Union
7
8
8
9
import mypy .api
9
10
import pytest
10
11
from _pytest ._code .code import ReprEntry , ReprFileLocation
11
12
from _pytest .config import Config
12
- from py ._path .local import LocalPath
13
13
14
14
from .message import Message , Severity
15
15
from .output_processing import OutputMismatch , diff_message_sequences
@@ -36,6 +36,8 @@ def __init__(self, item, errors: Iterable[OutputMismatch]):
36
36
37
37
38
38
class PytestMypyTestItem (pytest .Item ):
39
+ parent : "PytestMypyFile"
40
+
39
41
def __init__ (
40
42
self ,
41
43
name : str ,
@@ -72,7 +74,7 @@ def runtest(self) -> None:
72
74
if errors :
73
75
raise MypyAssertionError (item = self , errors = errors )
74
76
75
- def reportinfo (self ) -> Tuple [str , Optional [int ], str ]:
77
+ def reportinfo (self ) -> Tuple [Union [ "os.PathLike[ str]" , str ] , Optional [int ], str ]:
76
78
return self .parent .fspath , self .mypy_item .lineno , self .name
77
79
78
80
def repr_failure (self , excinfo , style = None ):
@@ -153,7 +155,8 @@ def run_mypy(self, item: MypyTestItem) -> Tuple[int, List[Message]]:
153
155
),
154
156
)
155
157
156
- def _run_mypy (self , filename : str ) -> MypyResult :
158
+ def _run_mypy (self , filename : Union [pathlib .Path , os .PathLike , str ]) -> MypyResult :
159
+ filename = pathlib .Path (filename )
157
160
with tempfile .TemporaryDirectory (prefix = "pytest-mypy-testing-" ) as tmp_dir_name :
158
161
159
162
mypy_cache_dir = os .path .join (tmp_dir_name , "mypy_cache" )
@@ -213,7 +216,7 @@ def _run_mypy(self, filename: str) -> MypyResult:
213
216
214
217
if PYTEST_VERSION_INFO < (7 ,):
215
218
216
- def pytest_collect_file (path : LocalPath , parent ):
219
+ def pytest_collect_file (path , parent ):
217
220
if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
218
221
file = PytestMypyFile .from_parent (parent = parent , fspath = path )
219
222
if file .mypy_file .items :
@@ -222,15 +225,15 @@ def pytest_collect_file(path: LocalPath, parent):
222
225
223
226
else :
224
227
225
- def pytest_collect_file (file_path , path : LocalPath , parent ): # type: ignore
228
+ def pytest_collect_file (file_path , path , parent ): # type: ignore
226
229
if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
227
230
file = PytestMypyFile .from_parent (parent = parent , path = file_path )
228
231
if file .mypy_file .items :
229
232
return file
230
233
return None
231
234
232
235
233
- def _is_pytest_test_file (path : LocalPath , parent ):
236
+ def _is_pytest_test_file (path , parent ):
234
237
"""Return `True` if *path* is considered to be a pytest test file."""
235
238
# Based on _pytest/python.py::pytest_collect_file
236
239
fn_patterns = parent .config .getini ("python_files" ) + ["__init__.py" ]
0 commit comments