From 30f1b81eb27f3589079c434688caa7d1e20e70f9 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 18 Mar 2021 23:08:03 +0100 Subject: [PATCH] address #8361 - introduce hook caller wrappers that enable backward compat --- src/_pytest/config/__init__.py | 4 +++- src/_pytest/config/compat.py | 41 ++++++++++++++++++++++++++++++++++ src/_pytest/main.py | 4 +++- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 src/_pytest/config/compat.py diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 624fbd02f..bdb68b3e2 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -917,8 +917,10 @@ class Config: :type: PytestPluginManager """ + from .compat import PathAwareHookProxy + self.trace = self.pluginmanager.trace.root.get("config") - self.hook = self.pluginmanager.hook + self.hook = PathAwareHookProxy(self.pluginmanager.hook) self._inicache: Dict[str, Any] = {} self._override_ini: Sequence[str] = () self._opt2dest: Dict[str, str] = {} diff --git a/src/_pytest/config/compat.py b/src/_pytest/config/compat.py new file mode 100644 index 000000000..2adc2d919 --- /dev/null +++ b/src/_pytest/config/compat.py @@ -0,0 +1,41 @@ +from typing import TYPE_CHECKING + +from _pytest.nodes import _imply_path + +if TYPE_CHECKING: + from ..compat import LEGACY_PATH + + +import functools + +# hookname: (Path, LEGACY_PATH) +imply_paths_hooks = { + "pytest_ignore_collect": ("fspath", "path"), + "pytest_collect_file": ("fspath", "path"), + "pytest_pycollect_makemodule": ("fspath", "path"), + "pytest_report_header": ("startpath", "startdir"), + "pytest_report_collectionfinish": ("startpath", "startdir"), +} + + +class PathAwareHookProxy: + def __init__(self, hook_caller): + self.__hook_caller = hook_caller + + def __getattr__(self, key): + if key not in imply_paths_hooks: + return getattr(self.__hook_caller, key) + else: + hook = getattr(self.__hook_caller, key) + path_var, fspath_var = imply_paths_hooks[key] + + @functools.wraps(hook) + def fixed_hook(**kw): + path_value = kw.pop(path_var, None) + fspath_value: "LEGACY_PATH" = kw.pop(fspath_var, None) + path_value, fspath_value = _imply_path(path_value, fspath_value) + kw[path_var] = path_value + kw[fspath_var] = fspath_value + return hook(**kw) + + return fixed_hook diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 06cfb1fd5..97a14ea59 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -551,7 +551,9 @@ class Session(nodes.FSCollector): remove_mods = pm._conftest_plugins.difference(my_conftestmodules) if remove_mods: # One or more conftests are not in use at this fspath. - proxy = FSHookProxy(pm, remove_mods) + from .config.compat import PathAwareHookProxy + + proxy = PathAwareHookProxy(FSHookProxy(pm, remove_mods)) else: # All plugins are active for this fspath. proxy = self.config.hook