test warnings and fix invocation bugs

This commit is contained in:
Ronny Pfannschmidt
2021-03-20 23:39:38 +01:00
parent 2994022753
commit 4ddf6c647c
4 changed files with 38 additions and 3 deletions

View File

@@ -1,8 +1,9 @@
import functools
import warnings
from pathlib import Path
from typing import Optional
from ..compat import LEGACY_PATH
from ..deprecated import HOOK_LEGACY_PATH_ARG
from _pytest.nodes import _imply_path
# hookname: (Path, LEGACY_PATH)
@@ -19,6 +20,9 @@ class PathAwareHookProxy:
def __init__(self, hook_caller):
self.__hook_caller = hook_caller
def __dir__(self):
return dir(self.__hook_caller)
def __getattr__(self, key):
if key not in imply_paths_hooks:
return getattr(self.__hook_caller, key)
@@ -26,13 +30,19 @@ class PathAwareHookProxy:
hook = getattr(self.__hook_caller, key)
path_var, fspath_var = imply_paths_hooks[key]
@functools.wraps(hook)
def fixed_hook(**kw):
path_value: Optional[Path] = kw.pop(path_var, None)
fspath_value: Optional[LEGACY_PATH] = kw.pop(fspath_var, None)
if fspath_value is not None:
warnings.warn(
HOOK_LEGACY_PATH_ARG.format(
pylib_path_arg=fspath_var, pathlib_path_arg=path_var
)
)
path_value, fspath_value = _imply_path(path_value, fspath_value)
kw[path_var] = path_value
kw[fspath_var] = fspath_value
return hook(**kw)
fixed_hook.__name__ = key
return fixed_hook

View File

@@ -95,6 +95,10 @@ NODE_FSPATH = UnformattedWarning(
"see https://docs.pytest.org/en/latest/deprecations.html#node-fspath-in-favor-of-pathlib-and-node-path",
)
HOOK_LEGACY_PATH_ARG = UnformattedWarning(
PytestDeprecationWarning,
"{pylib_path_arg} : py.path.local is deprecated, please use {pathlib_path_arg} : pathlib.Path",
)
# You want to make some `__init__` or function "private".
#
# def my_private_function(some, args):