Skip to content

Commit 30f1b81

Browse files
address #8361 - introduce hook caller wrappers that enable backward compat
1 parent 35df3e6 commit 30f1b81

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/_pytest/config/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,10 @@ def __init__(
917917
:type: PytestPluginManager
918918
"""
919919

920+
from .compat import PathAwareHookProxy
921+
920922
self.trace = self.pluginmanager.trace.root.get("config")
921-
self.hook = self.pluginmanager.hook
923+
self.hook = PathAwareHookProxy(self.pluginmanager.hook)
922924
self._inicache: Dict[str, Any] = {}
923925
self._override_ini: Sequence[str] = ()
924926
self._opt2dest: Dict[str, str] = {}

src/_pytest/config/compat.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import TYPE_CHECKING
2+
3+
from _pytest.nodes import _imply_path
4+
5+
if TYPE_CHECKING:
6+
from ..compat import LEGACY_PATH
7+
8+
9+
import functools
10+
11+
# hookname: (Path, LEGACY_PATH)
12+
imply_paths_hooks = {
13+
"pytest_ignore_collect": ("fspath", "path"),
14+
"pytest_collect_file": ("fspath", "path"),
15+
"pytest_pycollect_makemodule": ("fspath", "path"),
16+
"pytest_report_header": ("startpath", "startdir"),
17+
"pytest_report_collectionfinish": ("startpath", "startdir"),
18+
}
19+
20+
21+
class PathAwareHookProxy:
22+
def __init__(self, hook_caller):
23+
self.__hook_caller = hook_caller
24+
25+
def __getattr__(self, key):
26+
if key not in imply_paths_hooks:
27+
return getattr(self.__hook_caller, key)
28+
else:
29+
hook = getattr(self.__hook_caller, key)
30+
path_var, fspath_var = imply_paths_hooks[key]
31+
32+
@functools.wraps(hook)
33+
def fixed_hook(**kw):
34+
path_value = kw.pop(path_var, None)
35+
fspath_value: "LEGACY_PATH" = kw.pop(fspath_var, None)
36+
path_value, fspath_value = _imply_path(path_value, fspath_value)
37+
kw[path_var] = path_value
38+
kw[fspath_var] = fspath_value
39+
return hook(**kw)
40+
41+
return fixed_hook

src/_pytest/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ def gethookproxy(self, fspath: "os.PathLike[str]"):
551551
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
552552
if remove_mods:
553553
# One or more conftests are not in use at this fspath.
554-
proxy = FSHookProxy(pm, remove_mods)
554+
from .config.compat import PathAwareHookProxy
555+
556+
proxy = PathAwareHookProxy(FSHookProxy(pm, remove_mods))
555557
else:
556558
# All plugins are active for this fspath.
557559
proxy = self.config.hook

0 commit comments

Comments
 (0)