10
10
import pytest
11
11
from _pytest ._code .code import ReprEntry , ReprFileLocation
12
12
from _pytest .config import Config
13
+ from _pytest .python import path_matches_patterns
13
14
14
15
from .message import Message , Severity
15
16
from .output_processing import OutputMismatch , diff_message_sequences
@@ -57,12 +58,7 @@ def __init__(
57
58
58
59
@classmethod
59
60
def from_parent (cls , parent , name , mypy_item ):
60
- if PYTEST_VERSION_INFO < (5 , 4 ):
61
- return cls (
62
- parent = parent , name = name , config = parent .config , mypy_item = mypy_item
63
- )
64
- else :
65
- return super ().from_parent (parent = parent , name = name , mypy_item = mypy_item )
61
+ return super ().from_parent (parent = parent , name = name , mypy_item = mypy_item )
66
62
67
63
def runtest (self ) -> None :
68
64
returncode , actual_messages = self .parent .run_mypy (self .mypy_item )
@@ -75,14 +71,12 @@ def runtest(self) -> None:
75
71
raise MypyAssertionError (item = self , errors = errors )
76
72
77
73
def reportinfo (self ) -> Tuple [Union ["os.PathLike[str]" , str ], Optional [int ], str ]:
78
- return self .parent .fspath , self .mypy_item .lineno , self .name
74
+ return self .parent .path , self .mypy_item .lineno , self .name
79
75
80
76
def repr_failure (self , excinfo , style = None ):
81
77
if not excinfo .errisinstance (MypyAssertionError ):
82
78
return super ().repr_failure (excinfo , style = style ) # pragma: no cover
83
- reprfileloc_key = (
84
- "filelocrepr" if PYTEST_VERSION_INFO < (5 , 4 ) else "reprfileloc"
85
- )
79
+ reprfileloc_key = "reprfileloc"
86
80
exception_repr = excinfo .getrepr (style = "short" )
87
81
exception_repr .reprcrash .message = ""
88
82
exception_repr .reprtraceback .reprentries = [
@@ -93,7 +87,7 @@ def repr_failure(self, excinfo, style=None):
93
87
reprfuncargs = None ,
94
88
** {
95
89
reprfileloc_key : ReprFileLocation (
96
- path = self .parent .fspath ,
90
+ path = str ( self .parent .path ) ,
97
91
lineno = mismatch .lineno ,
98
92
message = mismatch .error_message ,
99
93
)
@@ -124,19 +118,12 @@ def __init__(
124
118
** kwargs ,
125
119
)
126
120
self .add_marker ("mypy" )
127
- if PYTEST_VERSION_INFO >= (7 ,):
128
- self .mypy_file = parse_file (self .path , config = config )
129
- else :
130
- self .mypy_file = parse_file (self .fspath , config = config )
121
+ self .mypy_file = parse_file (self .path , config = config )
131
122
self ._mypy_result : Optional [MypyResult ] = None
132
123
133
124
@classmethod
134
125
def from_parent (cls , parent , ** kwargs ):
135
- if PYTEST_VERSION_INFO < (5 , 4 ):
136
- config = getattr (parent , "config" , None )
137
- return cls (parent = parent , config = config , ** kwargs )
138
- else :
139
- return super ().from_parent (parent = parent , ** kwargs )
126
+ return super ().from_parent (parent = parent , ** kwargs )
140
127
141
128
def collect (self ) -> Iterator [PytestMypyTestItem ]:
142
129
for item in self .mypy_file .items :
@@ -146,7 +133,7 @@ def collect(self) -> Iterator[PytestMypyTestItem]:
146
133
147
134
def run_mypy (self , item : MypyTestItem ) -> Tuple [int , List [Message ]]:
148
135
if self ._mypy_result is None :
149
- self ._mypy_result = self ._run_mypy (self .fspath )
136
+ self ._mypy_result = self ._run_mypy (self .path )
150
137
return (
151
138
self ._mypy_result .returncode ,
152
139
sorted (
@@ -211,31 +198,21 @@ def _run_mypy(self, filename: Union[pathlib.Path, os.PathLike, str]) -> MypyResu
211
198
)
212
199
213
200
214
- if PYTEST_VERSION_INFO < (7 ,):
215
-
216
- def pytest_collect_file (path , parent ):
217
- if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
218
- file = PytestMypyFile .from_parent (parent = parent , fspath = path )
219
- if file .mypy_file .items :
220
- return file
221
- return None
222
-
223
- else :
224
-
225
- def pytest_collect_file (file_path , path , parent ): # type: ignore
226
- if path .ext == ".mypy-testing" or _is_pytest_test_file (path , parent ):
227
- file = PytestMypyFile .from_parent (parent = parent , path = file_path )
228
- if file .mypy_file .items :
229
- return file
230
- return None
201
+ def pytest_collect_file (file_path : pathlib .Path , parent ):
202
+ if file_path .suffix == ".mypy-testing" or _is_pytest_test_file (file_path , parent ):
203
+ file = PytestMypyFile .from_parent (parent = parent , path = file_path )
204
+ if file .mypy_file .items :
205
+ return file
206
+ return None
231
207
232
208
233
- def _is_pytest_test_file (path , parent ):
209
+ def _is_pytest_test_file (file_path : pathlib . Path , parent ):
234
210
"""Return `True` if *path* is considered to be a pytest test file."""
235
211
# Based on _pytest/python.py::pytest_collect_file
236
212
fn_patterns = parent .config .getini ("python_files" ) + ["__init__.py" ]
237
- return path .ext == ".py" and (
238
- parent .session .isinitpath (path ) or any (path .fnmatch (pat ) for pat in fn_patterns )
213
+ return file_path .suffix == ".py" and (
214
+ parent .session .isinitpath (file_path )
215
+ or path_matches_patterns (file_path , fn_patterns )
239
216
)
240
217
241
218
0 commit comments