@@ -403,60 +403,61 @@ def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None:
403403 self .path = Path (value )
404404
405405
406- @hookimpl
407- def pytest_configure (config : Config ) -> None :
408- import pytest
409-
410- mp = pytest .MonkeyPatch ()
411- config .add_cleanup (mp .undo )
412-
413- if config .pluginmanager .has_plugin ("tmpdir" ):
414- # Create TmpdirFactory and attach it to the config object.
415- #
416- # This is to comply with existing plugins which expect the handler to be
417- # available at pytest_configure time, but ideally should be moved entirely
418- # to the tmpdir_factory session fixture.
419- try :
420- tmp_path_factory = config ._tmp_path_factory # type: ignore[attr-defined]
421- except AttributeError :
422- # tmpdir plugin is blocked.
423- pass
424- else :
425- _tmpdirhandler = TempdirFactory (tmp_path_factory , _ispytest = True )
426- mp .setattr (config , "_tmpdirhandler" , _tmpdirhandler , raising = False )
427-
428- config .pluginmanager .register (LegacyTmpdirPlugin , "legacypath-tmpdir" )
406+ @hookimpl (tryfirst = True )
407+ def pytest_load_initial_conftests (early_config : Config ) -> None :
408+ """Monkeypatch legacy path attributes in several classes, as early as possible."""
409+ mp = MonkeyPatch ()
410+ early_config .add_cleanup (mp .undo )
429411
430412 # Add Cache.makedir().
431- mp .setattr (pytest . Cache , "makedir" , Cache_makedir , raising = False )
413+ mp .setattr (Cache , "makedir" , Cache_makedir , raising = False )
432414
433415 # Add FixtureRequest.fspath property.
434- mp .setattr (
435- pytest .FixtureRequest , "fspath" , property (FixtureRequest_fspath ), raising = False
436- )
416+ mp .setattr (FixtureRequest , "fspath" , property (FixtureRequest_fspath ), raising = False )
437417
438418 # Add TerminalReporter.startdir property.
439419 mp .setattr (
440420 TerminalReporter , "startdir" , property (TerminalReporter_startdir ), raising = False
441421 )
442422
443423 # Add Config.{invocation_dir,rootdir,inifile} properties.
444- mp .setattr (
445- pytest .Config , "invocation_dir" , property (Config_invocation_dir ), raising = False
446- )
447- mp .setattr (pytest .Config , "rootdir" , property (Config_rootdir ), raising = False )
448- mp .setattr (pytest .Config , "inifile" , property (Config_inifile ), raising = False )
424+ mp .setattr (Config , "invocation_dir" , property (Config_invocation_dir ), raising = False )
425+ mp .setattr (Config , "rootdir" , property (Config_rootdir ), raising = False )
426+ mp .setattr (Config , "inifile" , property (Config_inifile ), raising = False )
449427
450428 # Add Session.startdir property.
451- mp .setattr (pytest . Session , "startdir" , property (Session_stardir ), raising = False )
429+ mp .setattr (Session , "startdir" , property (Session_stardir ), raising = False )
452430
453431 # Add pathlist configuration type.
454- mp .setattr (pytest . Config , "_getini_unknown_type" , Config__getini_unknown_type )
432+ mp .setattr (Config , "_getini_unknown_type" , Config__getini_unknown_type )
455433
456434 # Add Node.fspath property.
457435 mp .setattr (Node , "fspath" , property (Node_fspath , Node_fspath_set ), raising = False )
458436
459437
438+ @hookimpl
439+ def pytest_configure (config : Config ) -> None :
440+ """Installs the LegacyTmpdirPlugin if the ``tmpdir`` plugin is also installed."""
441+ if config .pluginmanager .has_plugin ("tmpdir" ):
442+ mp = MonkeyPatch ()
443+ config .add_cleanup (mp .undo )
444+ # Create TmpdirFactory and attach it to the config object.
445+ #
446+ # This is to comply with existing plugins which expect the handler to be
447+ # available at pytest_configure time, but ideally should be moved entirely
448+ # to the tmpdir_factory session fixture.
449+ try :
450+ tmp_path_factory = config ._tmp_path_factory # type: ignore[attr-defined]
451+ except AttributeError :
452+ # tmpdir plugin is blocked.
453+ pass
454+ else :
455+ _tmpdirhandler = TempdirFactory (tmp_path_factory , _ispytest = True )
456+ mp .setattr (config , "_tmpdirhandler" , _tmpdirhandler , raising = False )
457+
458+ config .pluginmanager .register (LegacyTmpdirPlugin , "legacypath-tmpdir" )
459+
460+
460461@hookimpl
461462def pytest_plugin_registered (plugin : object , manager : PytestPluginManager ) -> None :
462463 # pytester is not loaded by default and is commonly loaded from a conftest,
0 commit comments