Make 'request' a reserved name for fixtures

This commit is contained in:
Bruno Oliveira 2019-06-30 13:14:38 -03:00
parent 85cc12e328
commit 0ed7aa2db6
8 changed files with 23 additions and 37 deletions

View File

@ -19,6 +19,8 @@ removed:
* ``RemovedInPytest4Warning`` warning type.
* ``request`` is now a reserved name for fixtures.
For more information consult
`Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__ in the docs.

View File

@ -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)

View File

@ -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

View File

@ -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."

View File

@ -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

View File

@ -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"
]
)

View File

@ -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",
]
)