From 7bb51b8ceb1df7dad1363ddbaa9abaeb333b4522 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 20 Oct 2018 12:09:44 -0300 Subject: [PATCH] Issue a warning when a fixture named 'request' is collected Fix #611 --- changelog/611.bugfix.rst | 2 ++ src/_pytest/deprecated.py | 10 +++++++++- src/_pytest/fixtures.py | 5 ++++- testing/deprecated_test.py | 12 ++++++++++++ .../deprecated/test_fixture_named_request.py | 10 ++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 changelog/611.bugfix.rst create mode 100644 testing/example_scripts/deprecated/test_fixture_named_request.py diff --git a/changelog/611.bugfix.rst b/changelog/611.bugfix.rst new file mode 100644 index 000000000..1b39f4aa9 --- /dev/null +++ b/changelog/611.bugfix.rst @@ -0,0 +1,2 @@ +Naming a fixture ``request`` will now raise a warning: the ``request`` fixture is internal and +should not be overwritten as it will lead to internal errors. diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 54886c999..e9e32616d 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -11,7 +11,11 @@ in case of warnings which need to format their messages. from __future__ import absolute_import, division, print_function -from _pytest.warning_types import UnformattedWarning, RemovedInPytest4Warning +from _pytest.warning_types import ( + UnformattedWarning, + RemovedInPytest4Warning, + PytestDeprecationWarning, +) MAIN_STR_ARGS = RemovedInPytest4Warning( @@ -55,6 +59,10 @@ FIXTURE_FUNCTION_CALL = UnformattedWarning( "See https://docs.pytest.org/en/latest/fixture.html for more information.", ) +FIXTURE_NAMED_REQUEST = PytestDeprecationWarning( + "'request' is a reserved name for fixtures and will raise an error in future versions" +) + CFG_PYTEST_SECTION = UnformattedWarning( RemovedInPytest4Warning, "[pytest] section in {filename} files is deprecated, use [tool:pytest] instead.", diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 29eda351f..565f8d061 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -32,7 +32,7 @@ from _pytest.compat import ( get_real_method, _PytestWrapper, ) -from _pytest.deprecated import FIXTURE_FUNCTION_CALL +from _pytest.deprecated import FIXTURE_FUNCTION_CALL, FIXTURE_NAMED_REQUEST from _pytest.outcomes import fail, TEST_OUTCOME FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}' @@ -1029,6 +1029,9 @@ class FixtureFunctionMarker(object): function = wrap_function_to_warning_if_called_directly(function, self) + name = self.name or function.__name__ + if name == "request": + warnings.warn(FIXTURE_NAMED_REQUEST) function._pytestfixturefunction = self return function diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 649ebcd38..0942c3a9c 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -4,6 +4,8 @@ import os import pytest +pytestmark = pytest.mark.pytester_example_path("deprecated") + @pytest.mark.filterwarnings("default") def test_yield_tests_deprecation(testdir): @@ -392,3 +394,13 @@ def test_pycollector_makeitem_is_deprecated(): with pytest.warns(RemovedInPytest4Warning): collector.makeitem("foo", "bar") assert collector.called + + +def test_fixture_named_request(testdir): + testdir.copy_example() + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*'request' is a reserved name for fixtures and will raise an error in future versions" + ] + ) diff --git a/testing/example_scripts/deprecated/test_fixture_named_request.py b/testing/example_scripts/deprecated/test_fixture_named_request.py new file mode 100644 index 000000000..75514bf8b --- /dev/null +++ b/testing/example_scripts/deprecated/test_fixture_named_request.py @@ -0,0 +1,10 @@ +import pytest + + +@pytest.fixture +def request(): + pass + + +def test(): + pass