From 0ed7aa2db678fb8c35a617525efd48c3352fc256 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 30 Jun 2019 13:14:38 -0300 Subject: [PATCH] Make 'request' a reserved name for fixtures --- changelog/5180.removal.rst | 2 ++ src/_pytest/compat.py | 4 +-- src/_pytest/config/__init__.py | 1 + src/_pytest/deprecated.py | 5 ---- src/_pytest/fixtures.py | 10 ++++--- testing/deprecated_test.py | 12 --------- .../test_fixture_named_request.py | 0 testing/python/fixtures.py | 26 ++++++++----------- 8 files changed, 23 insertions(+), 37 deletions(-) rename testing/example_scripts/{deprecated => fixtures}/test_fixture_named_request.py (100%) diff --git a/changelog/5180.removal.rst b/changelog/5180.removal.rst index 112c0150f..1174a7cba 100644 --- a/changelog/5180.removal.rst +++ b/changelog/5180.removal.rst @@ -19,6 +19,8 @@ removed: * ``RemovedInPytest4Warning`` warning type. +* ``request`` is now a reserved name for fixtures. + For more information consult `Deprecations and Removals `__ in the docs. diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index d238061b4..6b750e16f 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -50,11 +50,11 @@ def iscoroutinefunction(func): ) -def getlocation(function, curdir): +def getlocation(function, curdir=None): function = get_real_func(function) fn = py.path.local(inspect.getfile(function)) lineno = function.__code__.co_firstlineno - if fn.relto(curdir): + if curdir is not None and fn.relto(curdir): fn = fn.relto(curdir) return "%s:%d" % (fn, lineno + 1) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index cf6ecd385..d7a20ad50 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -18,6 +18,7 @@ from pluggy import PluginManager import _pytest._code import _pytest.assertion +import _pytest.deprecated import _pytest.hookspec # the extension point definitions from .exceptions import PrintHelp from .exceptions import UsageError diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index 39c8c23cd..c06908932 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -19,11 +19,6 @@ DEPRECATED_EXTERNAL_PLUGINS = { } -FIXTURE_NAMED_REQUEST = PytestDeprecationWarning( - "'request' is a reserved name for fixtures and will raise an error in future versions" -) - - FUNCARGNAMES = PytestDeprecationWarning( "The `funcargnames` attribute was an alias for `fixturenames`, " "since pytest 2.3 - use the newer attribute instead." diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 9a385fc42..46ef8c642 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -2,7 +2,6 @@ import functools import inspect import itertools import sys -import warnings from collections import defaultdict from collections import deque from collections import OrderedDict @@ -26,7 +25,6 @@ from _pytest.compat import getlocation from _pytest.compat import is_generator from _pytest.compat import NOTSET from _pytest.compat import safe_getattr -from _pytest.deprecated import FIXTURE_NAMED_REQUEST from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME @@ -971,7 +969,13 @@ class FixtureFunctionMarker: name = self.name or function.__name__ if name == "request": - warnings.warn(FIXTURE_NAMED_REQUEST) + location = getlocation(function) + fail( + "'request' is a reserved word for fixtures, use another name:\n {}".format( + location + ), + pytrace=False, + ) function._pytestfixturefunction = self return function diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index e4a8f72bc..97b88e939 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -1,8 +1,6 @@ import pytest from _pytest import deprecated -pytestmark = pytest.mark.pytester_example_path("deprecated") - @pytest.mark.filterwarnings("default") def test_resultlog_is_deprecated(testdir): @@ -46,13 +44,3 @@ def test_external_plugins_integrated(testdir, plugin): with pytest.warns(pytest.PytestConfigWarning): testdir.parseconfig("-p", plugin) - - -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/fixtures/test_fixture_named_request.py similarity index 100% rename from testing/example_scripts/deprecated/test_fixture_named_request.py rename to testing/example_scripts/fixtures/test_fixture_named_request.py diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index c2484a8a2..e5bc25a1c 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1126,21 +1126,6 @@ class TestFixtureUsages: values = reprec.getfailedcollections() assert len(values) == 1 - def test_request_can_be_overridden(self, testdir): - testdir.makepyfile( - """ - import pytest - @pytest.fixture() - def request(request): - request.a = 1 - return request - def test_request(request): - assert request.a == 1 - """ - ) - reprec = testdir.inline_run("-Wignore::pytest.PytestDeprecationWarning") - reprec.assertoutcome(passed=1) - def test_usefixtures_marker(self, testdir): testdir.makepyfile( """ @@ -3973,3 +3958,14 @@ def test_fixture_param_shadowing(testdir): result.stdout.fnmatch_lines(["*::test_normal_fixture[[]a[]]*"]) result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"]) result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"]) + + +def test_fixture_named_request(testdir): + testdir.copy_example("fixtures/test_fixture_named_request.py") + result = testdir.runpytest() + result.stdout.fnmatch_lines( + [ + "*'request' is a reserved word for fixtures, use another name:", + " *test_fixture_named_request.py:5", + ] + )