22# SPDX-License-Identifier: Apache-2.0 OR MIT
33
44import os
5+ import pathlib
56import tempfile
6- from typing import Iterable , Iterator , List , NamedTuple , Optional , Tuple
7+ from typing import Iterable , Iterator , List , NamedTuple , Optional , Tuple , Union
78
89import mypy .api
910import pytest
1011from _pytest ._code .code import ReprEntry , ReprFileLocation
1112from _pytest .config import Config
12- from py ._path .local import LocalPath
1313
1414from .message import Message , Severity
1515from .output_processing import OutputMismatch , diff_message_sequences
@@ -36,6 +36,8 @@ def __init__(self, item, errors: Iterable[OutputMismatch]):
3636
3737
3838class PytestMypyTestItem (pytest .Item ):
39+ parent : "PytestMypyFile"
40+
3941 def __init__ (
4042 self ,
4143 name : str ,
@@ -72,7 +74,7 @@ def runtest(self) -> None:
7274 if errors :
7375 raise MypyAssertionError (item = self , errors = errors )
7476
75- def reportinfo (self ) -> Tuple [str , Optional [int ], str ]:
77+ def reportinfo (self ) -> Tuple [Union [ "os.PathLike[ str]" , str ] , Optional [int ], str ]:
7678 return self .parent .fspath , self .mypy_item .lineno , self .name
7779
7880 def repr_failure (self , excinfo , style = None ):
@@ -153,7 +155,8 @@ def run_mypy(self, item: MypyTestItem) -> Tuple[int, List[Message]]:
153155 ),
154156 )
155157
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 )
157160 with tempfile .TemporaryDirectory (prefix = "pytest-mypy-testing-" ) as tmp_dir_name :
158161
159162 mypy_cache_dir = os .path .join (tmp_dir_name , "mypy_cache" )
@@ -178,9 +181,6 @@ def _run_mypy(self, filename: str) -> MypyResult:
178181
179182 lines = (out + err ).splitlines ()
180183
181- # for line in lines:
182- # print("%%%%", line)
183-
184184 file_messages = [
185185 msg
186186 for msg in map (Message .from_output , lines )
@@ -213,7 +213,7 @@ def _run_mypy(self, filename: str) -> MypyResult:
213213
214214if PYTEST_VERSION_INFO < (7 ,):
215215
216- def pytest_collect_file (path : LocalPath , parent ):
216+ def pytest_collect_file (path , parent ):
217217 if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
218218 file = PytestMypyFile .from_parent (parent = parent , fspath = path )
219219 if file .mypy_file .items :
@@ -222,15 +222,15 @@ def pytest_collect_file(path: LocalPath, parent):
222222
223223else :
224224
225- def pytest_collect_file (file_path , path : LocalPath , parent ): # type: ignore
225+ def pytest_collect_file (file_path , path , parent ): # type: ignore
226226 if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
227227 file = PytestMypyFile .from_parent (parent = parent , path = file_path )
228228 if file .mypy_file .items :
229229 return file
230230 return None
231231
232232
233- def _is_pytest_test_file (path : LocalPath , parent ):
233+ def _is_pytest_test_file (path , parent ):
234234 """Return `True` if *path* is considered to be a pytest test file."""
235235 # Based on _pytest/python.py::pytest_collect_file
236236 fn_patterns = parent .config .getini ("python_files" ) + ["__init__.py" ]
@@ -259,4 +259,4 @@ def _add_reveal_type_to_builtins():
259259 import builtins
260260
261261 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