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" )
@@ -178,9 +181,6 @@ def _run_mypy(self, filename: str) -> MypyResult:
178
181
179
182
lines = (out + err ).splitlines ()
180
183
181
- # for line in lines:
182
- # print("%%%%", line)
183
-
184
184
file_messages = [
185
185
msg
186
186
for msg in map (Message .from_output , lines )
@@ -213,7 +213,7 @@ def _run_mypy(self, filename: str) -> MypyResult:
213
213
214
214
if PYTEST_VERSION_INFO < (7 ,):
215
215
216
- def pytest_collect_file (path : LocalPath , parent ):
216
+ def pytest_collect_file (path , parent ):
217
217
if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
218
218
file = PytestMypyFile .from_parent (parent = parent , fspath = path )
219
219
if file .mypy_file .items :
@@ -222,15 +222,15 @@ def pytest_collect_file(path: LocalPath, parent):
222
222
223
223
else :
224
224
225
- def pytest_collect_file (file_path , path : LocalPath , parent ): # type: ignore
225
+ def pytest_collect_file (file_path , path , parent ): # type: ignore
226
226
if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
227
227
file = PytestMypyFile .from_parent (parent = parent , path = file_path )
228
228
if file .mypy_file .items :
229
229
return file
230
230
return None
231
231
232
232
233
- def _is_pytest_test_file (path : LocalPath , parent ):
233
+ def _is_pytest_test_file (path , parent ):
234
234
"""Return `True` if *path* is considered to be a pytest test file."""
235
235
# Based on _pytest/python.py::pytest_collect_file
236
236
fn_patterns = parent .config .getini ("python_files" ) + ["__init__.py" ]
@@ -259,4 +259,4 @@ def _add_reveal_type_to_builtins():
259
259
import builtins
260
260
261
261
if not hasattr (builtins , "reveal_type" ):
262
- setattr (builtins , "reveal_type" , lambda x : x )
262
+ setattr (builtins , "reveal_type" , lambda x : x ) # noqa: B010
0 commit comments