Skip to content

Commit 4ddf6c6

Browse files
test warnings and fix invocation bugs
1 parent 2994022 commit 4ddf6c6

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ repos:
8989
types: [python]
9090
- id: py-path-deprecated
9191
name: py.path usage is deprecated
92+
exclude: docs|src/_pytest/deprecated.py|testing/deprecated_test.py
9293
language: pygrep
9394
entry: \bpy\.path\.local
94-
exclude: docs
9595
types: [python]

src/_pytest/config/compat.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import functools
1+
import warnings
22
from pathlib import Path
33
from typing import Optional
44

55
from ..compat import LEGACY_PATH
6+
from ..deprecated import HOOK_LEGACY_PATH_ARG
67
from _pytest.nodes import _imply_path
78

89
# hookname: (Path, LEGACY_PATH)
@@ -19,20 +20,29 @@ class PathAwareHookProxy:
1920
def __init__(self, hook_caller):
2021
self.__hook_caller = hook_caller
2122

23+
def __dir__(self):
24+
return dir(self.__hook_caller)
25+
2226
def __getattr__(self, key):
2327
if key not in imply_paths_hooks:
2428
return getattr(self.__hook_caller, key)
2529
else:
2630
hook = getattr(self.__hook_caller, key)
2731
path_var, fspath_var = imply_paths_hooks[key]
2832

29-
@functools.wraps(hook)
3033
def fixed_hook(**kw):
3134
path_value: Optional[Path] = kw.pop(path_var, None)
3235
fspath_value: Optional[LEGACY_PATH] = kw.pop(fspath_var, None)
36+
if fspath_value is not None:
37+
warnings.warn(
38+
HOOK_LEGACY_PATH_ARG.format(
39+
pylib_path_arg=fspath_var, pathlib_path_arg=path_var
40+
)
41+
)
3342
path_value, fspath_value = _imply_path(path_value, fspath_value)
3443
kw[path_var] = path_value
3544
kw[fspath_var] = fspath_value
3645
return hook(**kw)
3746

47+
fixed_hook.__name__ = key
3848
return fixed_hook

src/_pytest/deprecated.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
"see https://docs.pytest.org/en/latest/deprecations.html#node-fspath-in-favor-of-pathlib-and-node-path",
9696
)
9797

98+
HOOK_LEGACY_PATH_ARG = UnformattedWarning(
99+
PytestDeprecationWarning,
100+
"{pylib_path_arg} : py.path.local is deprecated, please use {pathlib_path_arg} : pathlib.Path",
101+
)
98102
# You want to make some `__init__` or function "private".
99103
#
100104
# def my_private_function(some, args):

testing/deprecated_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
import pytest
66
from _pytest import deprecated
7+
from _pytest.compat import legacy_path
78
from _pytest.pytester import Pytester
9+
from pytest import PytestDeprecationWarning
810

911

1012
@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
@@ -153,3 +155,22 @@ def test_raising_unittest_skiptest_during_collection_is_deprecated(
153155
"*PytestDeprecationWarning: Raising unittest.SkipTest*",
154156
]
155157
)
158+
159+
160+
def test_hookproxy_warnings_for_fspath(pytestconfig, tmp_path, request):
161+
path = legacy_path(tmp_path)
162+
163+
with pytest.warns(
164+
PytestDeprecationWarning,
165+
match="path : py.path.local is deprecated, please use fspath : pathlib.Path",
166+
):
167+
pytestconfig.hook.pytest_ignore_collect(
168+
config=pytestconfig, path=path, fspath=tmp_path
169+
)
170+
with pytest.warns(
171+
PytestDeprecationWarning,
172+
match="path : py.path.local is deprecated, please use fspath : pathlib.Path",
173+
):
174+
request.node.ihook.pytest_ignore_collect(
175+
config=pytestconfig, path=path, fspath=tmp_path
176+
)

0 commit comments

Comments
 (0)