From e3d412d1f4d873bce356055e8c736a1220bed6bb Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 29 Jul 2018 21:30:49 -0300 Subject: [PATCH 1/4] Warn when implementations exist for pytest_namespace hook This hook has been deprecated and will be removed in the future. Fix #2639 --- changelog/2639.removal.rst | 3 +++ setup.py | 2 +- src/_pytest/hookspec.py | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 changelog/2639.removal.rst diff --git a/changelog/2639.removal.rst b/changelog/2639.removal.rst new file mode 100644 index 000000000..b6c1eed8a --- /dev/null +++ b/changelog/2639.removal.rst @@ -0,0 +1,3 @@ +``pytest_namespace`` has been deprecated. + +Plugins who need this feature are suggested to import ``pytest`` and set attributes explicitly during ``pytest_configure``. diff --git a/setup.py b/setup.py index b76825aff..b5b9dfecf 100644 --- a/setup.py +++ b/setup.py @@ -69,7 +69,7 @@ def main(): # if _PYTEST_SETUP_SKIP_PLUGGY_DEP is set, skip installing pluggy; # used by tox.ini to test with pluggy master if "_PYTEST_SETUP_SKIP_PLUGGY_DEP" not in os.environ: - install_requires.append("pluggy>=0.5,<0.8") + install_requires.append("pluggy>=0.7") environment_marker_support_level = get_environment_marker_support_level() if environment_marker_support_level >= 2: install_requires.append('funcsigs;python_version<"3.0"') diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index fec43a400..88b6ee455 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -1,6 +1,7 @@ """ hook specifications for pytest plugins, invoked from main.py and builtin plugins. """ from pluggy import HookspecMarker +from .deprecated import RemovedInPytest4Warning hookspec = HookspecMarker("pytest") @@ -22,10 +23,14 @@ def pytest_addhooks(pluginmanager): """ -@hookspec(historic=True) +@hookspec( + historic=True, + warn_on_impl=RemovedInPytest4Warning( + "pytest_namespace is deprecated and will be removed soon" + ), +) def pytest_namespace(): """ - (**Deprecated**) this hook causes direct monkeypatching on pytest, its use is strongly discouraged return dict of name->object to be made globally available in the pytest namespace. @@ -33,6 +38,12 @@ def pytest_namespace(): .. note:: This hook is incompatible with ``hookwrapper=True``. + + .. warning:: + This hook has been **deprecated** and will be removed in pytest 4.0. + + Plugins who need this feature are suggested + to import ``pytest`` and set attributes explicitly during ``pytest_configure``. """ From cf6d8e7e53f6fa4544e12d60b270c172a2e01ecf Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 30 Jul 2018 12:16:42 -0300 Subject: [PATCH 2/4] Fix test and update warning in pytest_namespace docs --- src/_pytest/hookspec.py | 11 +++++++++-- testing/test_pluginmanager.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index 88b6ee455..cc577e8c3 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -42,8 +42,15 @@ def pytest_namespace(): .. warning:: This hook has been **deprecated** and will be removed in pytest 4.0. - Plugins who need this feature are suggested - to import ``pytest`` and set attributes explicitly during ``pytest_configure``. + Plugins whose users depend on the current namespace functionality should prepare to migrate to a + namespace they actually own. + + To support the migration its suggested to trigger ``DeprecationWarnings`` for objects they put into the + pytest namespace. + + An stopgap measure to avoid the warning is to monkeypatch the ``pytest`` module, but just as the + ``pytest_namespace`` hook this should be seen as a temporary measure to be removed in future versions after + an appropriate transition period. """ diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index c24314d22..958dfc650 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -66,6 +66,7 @@ class TestPytestPluginInteractions(object): result = testdir.runpython(p) assert result.ret == 0 + @pytest.mark.filterwarnings("ignore:pytest_namespace is deprecated") def test_do_ext_namespace(self, testdir): testdir.makeconftest( """ From 953a618102782daceb229af3b8788b71560b1f0d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 30 Jul 2018 12:18:37 -0300 Subject: [PATCH 3/4] Update CHANGELOG entry about pytest_namespace deprecation --- changelog/2639.removal.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog/2639.removal.rst b/changelog/2639.removal.rst index b6c1eed8a..3ae6aa4db 100644 --- a/changelog/2639.removal.rst +++ b/changelog/2639.removal.rst @@ -1,3 +1,4 @@ ``pytest_namespace`` has been deprecated. -Plugins who need this feature are suggested to import ``pytest`` and set attributes explicitly during ``pytest_configure``. +See the documentation for ``pytest_namespace`` hook for suggestions on how to deal +with this in plugins which use this functionality. From 8609f8d25a1f9a59df835bf51bdf86953c127f00 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Mon, 30 Jul 2018 14:25:29 -0300 Subject: [PATCH 4/4] Move warning definition to deprecated module --- src/_pytest/deprecated.py | 4 ++++ src/_pytest/hookspec.py | 10 +++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index d32b675ae..20f1cc25b 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -71,3 +71,7 @@ PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = RemovedInPytest4Warning( "because it affects the entire directory tree in a non-explicit way.\n" "Please move it to the top level conftest file instead." ) + +PYTEST_NAMESPACE = RemovedInPytest4Warning( + "pytest_namespace is deprecated and will be removed soon" +) diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index cc577e8c3..e2969110a 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -1,7 +1,8 @@ """ hook specifications for pytest plugins, invoked from main.py and builtin plugins. """ from pluggy import HookspecMarker -from .deprecated import RemovedInPytest4Warning +from .deprecated import PYTEST_NAMESPACE + hookspec = HookspecMarker("pytest") @@ -23,12 +24,7 @@ def pytest_addhooks(pluginmanager): """ -@hookspec( - historic=True, - warn_on_impl=RemovedInPytest4Warning( - "pytest_namespace is deprecated and will be removed soon" - ), -) +@hookspec(historic=True, warn_on_impl=PYTEST_NAMESPACE) def pytest_namespace(): """ return dict of name->object to be made globally available in