10
10
import attr
11
11
from iniconfig import SectionWrapper
12
12
13
- import pytest
13
+ from _pytest . cacheprovider import Cache
14
14
from _pytest .compat import final
15
15
from _pytest .compat import LEGACY_PATH
16
16
from _pytest .compat import legacy_path
17
+ from _pytest .config import Config
18
+ from _pytest .config import hookimpl
19
+ from _pytest .config import PytestPluginManager
17
20
from _pytest .deprecated import check_ispytest
21
+ from _pytest .fixtures import fixture
22
+ from _pytest .fixtures import FixtureRequest
23
+ from _pytest .main import Session
24
+ from _pytest .monkeypatch import MonkeyPatch
25
+ from _pytest .nodes import Collector
26
+ from _pytest .nodes import Item
18
27
from _pytest .nodes import Node
28
+ from _pytest .pytester import HookRecorder
29
+ from _pytest .pytester import Pytester
30
+ from _pytest .pytester import RunResult
19
31
from _pytest .terminal import TerminalReporter
32
+ from _pytest .tmpdir import TempPathFactory
20
33
21
34
if TYPE_CHECKING :
22
35
from typing_extensions import Final
@@ -35,10 +48,10 @@ class Testdir:
35
48
36
49
__test__ = False
37
50
38
- CLOSE_STDIN : "Final" = pytest . Pytester .CLOSE_STDIN
39
- TimeoutExpired : "Final" = pytest . Pytester .TimeoutExpired
51
+ CLOSE_STDIN : "Final" = Pytester .CLOSE_STDIN
52
+ TimeoutExpired : "Final" = Pytester .TimeoutExpired
40
53
41
- def __init__ (self , pytester : pytest . Pytester , * , _ispytest : bool = False ) -> None :
54
+ def __init__ (self , pytester : Pytester , * , _ispytest : bool = False ) -> None :
42
55
check_ispytest (_ispytest )
43
56
self ._pytester = pytester
44
57
@@ -64,10 +77,10 @@ def plugins(self, plugins):
64
77
self ._pytester .plugins = plugins
65
78
66
79
@property
67
- def monkeypatch (self ) -> pytest . MonkeyPatch :
80
+ def monkeypatch (self ) -> MonkeyPatch :
68
81
return self ._pytester ._monkeypatch
69
82
70
- def make_hook_recorder (self , pluginmanager ) -> pytest . HookRecorder :
83
+ def make_hook_recorder (self , pluginmanager ) -> HookRecorder :
71
84
"""See :meth:`Pytester.make_hook_recorder`."""
72
85
return self ._pytester .make_hook_recorder (pluginmanager )
73
86
@@ -131,19 +144,15 @@ def copy_example(self, name=None) -> LEGACY_PATH:
131
144
"""See :meth:`Pytester.copy_example`."""
132
145
return legacy_path (self ._pytester .copy_example (name ))
133
146
134
- def getnode (
135
- self , config : pytest .Config , arg
136
- ) -> Optional [Union [pytest .Item , pytest .Collector ]]:
147
+ def getnode (self , config : Config , arg ) -> Optional [Union [Item , Collector ]]:
137
148
"""See :meth:`Pytester.getnode`."""
138
149
return self ._pytester .getnode (config , arg )
139
150
140
151
def getpathnode (self , path ):
141
152
"""See :meth:`Pytester.getpathnode`."""
142
153
return self ._pytester .getpathnode (path )
143
154
144
- def genitems (
145
- self , colitems : List [Union [pytest .Item , pytest .Collector ]]
146
- ) -> List [pytest .Item ]:
155
+ def genitems (self , colitems : List [Union [Item , Collector ]]) -> List [Item ]:
147
156
"""See :meth:`Pytester.genitems`."""
148
157
return self ._pytester .genitems (colitems )
149
158
@@ -165,19 +174,19 @@ def inline_run(self, *args, plugins=(), no_reraise_ctrlc: bool = False):
165
174
* args , plugins = plugins , no_reraise_ctrlc = no_reraise_ctrlc
166
175
)
167
176
168
- def runpytest_inprocess (self , * args , ** kwargs ) -> pytest . RunResult :
177
+ def runpytest_inprocess (self , * args , ** kwargs ) -> RunResult :
169
178
"""See :meth:`Pytester.runpytest_inprocess`."""
170
179
return self ._pytester .runpytest_inprocess (* args , ** kwargs )
171
180
172
- def runpytest (self , * args , ** kwargs ) -> pytest . RunResult :
181
+ def runpytest (self , * args , ** kwargs ) -> RunResult :
173
182
"""See :meth:`Pytester.runpytest`."""
174
183
return self ._pytester .runpytest (* args , ** kwargs )
175
184
176
- def parseconfig (self , * args ) -> pytest . Config :
185
+ def parseconfig (self , * args ) -> Config :
177
186
"""See :meth:`Pytester.parseconfig`."""
178
187
return self ._pytester .parseconfig (* args )
179
188
180
- def parseconfigure (self , * args ) -> pytest . Config :
189
+ def parseconfigure (self , * args ) -> Config :
181
190
"""See :meth:`Pytester.parseconfigure`."""
182
191
return self ._pytester .parseconfigure (* args )
183
192
@@ -196,8 +205,8 @@ def getmodulecol(self, source, configargs=(), withinit=False):
196
205
)
197
206
198
207
def collect_by_name (
199
- self , modcol : pytest . Collector , name : str
200
- ) -> Optional [Union [pytest . Item , pytest . Collector ]]:
208
+ self , modcol : Collector , name : str
209
+ ) -> Optional [Union [Item , Collector ]]:
201
210
"""See :meth:`Pytester.collect_by_name`."""
202
211
return self ._pytester .collect_by_name (modcol , name )
203
212
@@ -212,19 +221,19 @@ def popen(
212
221
"""See :meth:`Pytester.popen`."""
213
222
return self ._pytester .popen (cmdargs , stdout , stderr , stdin , ** kw )
214
223
215
- def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ) -> pytest . RunResult :
224
+ def run (self , * cmdargs , timeout = None , stdin = CLOSE_STDIN ) -> RunResult :
216
225
"""See :meth:`Pytester.run`."""
217
226
return self ._pytester .run (* cmdargs , timeout = timeout , stdin = stdin )
218
227
219
- def runpython (self , script ) -> pytest . RunResult :
228
+ def runpython (self , script ) -> RunResult :
220
229
"""See :meth:`Pytester.runpython`."""
221
230
return self ._pytester .runpython (script )
222
231
223
232
def runpython_c (self , command ):
224
233
"""See :meth:`Pytester.runpython_c`."""
225
234
return self ._pytester .runpython_c (command )
226
235
227
- def runpytest_subprocess (self , * args , timeout = None ) -> pytest . RunResult :
236
+ def runpytest_subprocess (self , * args , timeout = None ) -> RunResult :
228
237
"""See :meth:`Pytester.runpytest_subprocess`."""
229
238
return self ._pytester .runpytest_subprocess (* args , timeout = timeout )
230
239
@@ -245,13 +254,10 @@ def __str__(self) -> str:
245
254
return str (self .tmpdir )
246
255
247
256
248
- pytest .Testdir = Testdir # type: ignore[attr-defined]
249
-
250
-
251
257
class LegacyTestdirPlugin :
252
258
@staticmethod
253
- @pytest . fixture
254
- def testdir (pytester : pytest . Pytester ) -> Testdir :
259
+ @fixture
260
+ def testdir (pytester : Pytester ) -> Testdir :
255
261
"""
256
262
Identical to :fixture:`pytester`, and provides an instance whose methods return
257
263
legacy ``LEGACY_PATH`` objects instead when applicable.
@@ -267,10 +273,10 @@ class TempdirFactory:
267
273
"""Backward compatibility wrapper that implements :class:``_pytest.compat.LEGACY_PATH``
268
274
for :class:``TempPathFactory``."""
269
275
270
- _tmppath_factory : pytest . TempPathFactory
276
+ _tmppath_factory : TempPathFactory
271
277
272
278
def __init__ (
273
- self , tmppath_factory : pytest . TempPathFactory , * , _ispytest : bool = False
279
+ self , tmppath_factory : TempPathFactory , * , _ispytest : bool = False
274
280
) -> None :
275
281
check_ispytest (_ispytest )
276
282
self ._tmppath_factory = tmppath_factory
@@ -284,19 +290,16 @@ def getbasetemp(self) -> LEGACY_PATH:
284
290
return legacy_path (self ._tmppath_factory .getbasetemp ().resolve ())
285
291
286
292
287
- pytest .TempdirFactory = TempdirFactory # type: ignore[attr-defined]
288
-
289
-
290
293
class LegacyTmpdirPlugin :
291
294
@staticmethod
292
- @pytest . fixture (scope = "session" )
293
- def tmpdir_factory (request : pytest . FixtureRequest ) -> TempdirFactory :
295
+ @fixture (scope = "session" )
296
+ def tmpdir_factory (request : FixtureRequest ) -> TempdirFactory :
294
297
"""Return a :class:`pytest.TempdirFactory` instance for the test session."""
295
298
# Set dynamically by pytest_configure().
296
299
return request .config ._tmpdirhandler # type: ignore
297
300
298
301
@staticmethod
299
- @pytest . fixture
302
+ @fixture
300
303
def tmpdir (tmp_path : Path ) -> LEGACY_PATH :
301
304
"""Return a temporary directory path object which is unique to each test
302
305
function invocation, created as a sub directory of the base temporary
@@ -314,15 +317,15 @@ def tmpdir(tmp_path: Path) -> LEGACY_PATH:
314
317
return legacy_path (tmp_path )
315
318
316
319
317
- def Cache_makedir (self : pytest . Cache , name : str ) -> LEGACY_PATH :
320
+ def Cache_makedir (self : Cache , name : str ) -> LEGACY_PATH :
318
321
"""Return a directory path object with the given name.
319
322
320
323
Same as :func:`mkdir`, but returns a legacy py path instance.
321
324
"""
322
325
return legacy_path (self .mkdir (name ))
323
326
324
327
325
- def FixtureRequest_fspath (self : pytest . FixtureRequest ) -> LEGACY_PATH :
328
+ def FixtureRequest_fspath (self : FixtureRequest ) -> LEGACY_PATH :
326
329
"""(deprecated) The file system path of the test module which collected this test."""
327
330
return legacy_path (self .path )
328
331
@@ -337,7 +340,7 @@ def TerminalReporter_startdir(self: TerminalReporter) -> LEGACY_PATH:
337
340
return legacy_path (self .startpath )
338
341
339
342
340
- def Config_invocation_dir (self : pytest . Config ) -> LEGACY_PATH :
343
+ def Config_invocation_dir (self : Config ) -> LEGACY_PATH :
341
344
"""The directory from which pytest was invoked.
342
345
343
346
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
@@ -348,7 +351,7 @@ def Config_invocation_dir(self: pytest.Config) -> LEGACY_PATH:
348
351
return legacy_path (str (self .invocation_params .dir ))
349
352
350
353
351
- def Config_rootdir (self : pytest . Config ) -> LEGACY_PATH :
354
+ def Config_rootdir (self : Config ) -> LEGACY_PATH :
352
355
"""The path to the :ref:`rootdir <rootdir>`.
353
356
354
357
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
@@ -358,7 +361,7 @@ def Config_rootdir(self: pytest.Config) -> LEGACY_PATH:
358
361
return legacy_path (str (self .rootpath ))
359
362
360
363
361
- def Config_inifile (self : pytest . Config ) -> Optional [LEGACY_PATH ]:
364
+ def Config_inifile (self : Config ) -> Optional [LEGACY_PATH ]:
362
365
"""The path to the :ref:`configfile <configfiles>`.
363
366
364
367
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
@@ -368,7 +371,7 @@ def Config_inifile(self: pytest.Config) -> Optional[LEGACY_PATH]:
368
371
return legacy_path (str (self .inipath )) if self .inipath else None
369
372
370
373
371
- def Session_stardir (self : pytest . Session ) -> LEGACY_PATH :
374
+ def Session_stardir (self : Session ) -> LEGACY_PATH :
372
375
"""The path from which pytest was invoked.
373
376
374
377
Prefer to use ``startpath`` which is a :class:`pathlib.Path`.
@@ -400,8 +403,10 @@ def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
400
403
self .path = Path (value )
401
404
402
405
403
- @pytest .hookimpl
404
- def pytest_configure (config : pytest .Config ) -> None :
406
+ @hookimpl
407
+ def pytest_configure (config : Config ) -> None :
408
+ import pytest
409
+
405
410
mp = pytest .MonkeyPatch ()
406
411
config .add_cleanup (mp .undo )
407
412
@@ -452,10 +457,8 @@ def pytest_configure(config: pytest.Config) -> None:
452
457
mp .setattr (Node , "fspath" , property (Node_fspath , Node_fspath_set ), raising = False )
453
458
454
459
455
- @pytest .hookimpl
456
- def pytest_plugin_registered (
457
- plugin : object , manager : pytest .PytestPluginManager
458
- ) -> None :
460
+ @hookimpl
461
+ def pytest_plugin_registered (plugin : object , manager : PytestPluginManager ) -> None :
459
462
# pytester is not loaded by default and is commonly loaded from a conftest,
460
463
# so checking for it in `pytest_configure` is not enough.
461
464
is_pytester = plugin is manager .get_plugin ("pytester" )
0 commit comments