Skip to content

Commit f30911d

Browse files
authored
Merge pull request #4209 from nicoddemus/fixture-named-request
Issue a warning when a fixture named 'request' is collected
2 parents b432f12 + 7bb51b8 commit f30911d

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

changelog/611.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Naming a fixture ``request`` will now raise a warning: the ``request`` fixture is internal and
2+
should not be overwritten as it will lead to internal errors.

src/_pytest/deprecated.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from __future__ import absolute_import, division, print_function
1212

1313

14-
from _pytest.warning_types import UnformattedWarning, RemovedInPytest4Warning
14+
from _pytest.warning_types import (
15+
UnformattedWarning,
16+
RemovedInPytest4Warning,
17+
PytestDeprecationWarning,
18+
)
1519

1620

1721
MAIN_STR_ARGS = RemovedInPytest4Warning(
@@ -55,6 +59,10 @@
5559
"See https://docs.pytest.org/en/latest/fixture.html for more information.",
5660
)
5761

62+
FIXTURE_NAMED_REQUEST = PytestDeprecationWarning(
63+
"'request' is a reserved name for fixtures and will raise an error in future versions"
64+
)
65+
5866
CFG_PYTEST_SECTION = UnformattedWarning(
5967
RemovedInPytest4Warning,
6068
"[pytest] section in {filename} files is deprecated, use [tool:pytest] instead.",

src/_pytest/fixtures.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
get_real_method,
3333
_PytestWrapper,
3434
)
35-
from _pytest.deprecated import FIXTURE_FUNCTION_CALL
35+
from _pytest.deprecated import FIXTURE_FUNCTION_CALL, FIXTURE_NAMED_REQUEST
3636
from _pytest.outcomes import fail, TEST_OUTCOME
3737

3838
FIXTURE_MSG = 'fixtures cannot have "pytest_funcarg__" prefix and be decorated with @pytest.fixture:\n{}'
@@ -1029,6 +1029,9 @@ def __call__(self, function):
10291029

10301030
function = wrap_function_to_warning_if_called_directly(function, self)
10311031

1032+
name = self.name or function.__name__
1033+
if name == "request":
1034+
warnings.warn(FIXTURE_NAMED_REQUEST)
10321035
function._pytestfixturefunction = self
10331036
return function
10341037

testing/deprecated_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
pytestmark = pytest.mark.pytester_example_path("deprecated")
8+
79

810
@pytest.mark.filterwarnings("default")
911
def test_yield_tests_deprecation(testdir):
@@ -392,3 +394,13 @@ def _makeitem(self, *k):
392394
with pytest.warns(RemovedInPytest4Warning):
393395
collector.makeitem("foo", "bar")
394396
assert collector.called
397+
398+
399+
def test_fixture_named_request(testdir):
400+
testdir.copy_example()
401+
result = testdir.runpytest()
402+
result.stdout.fnmatch_lines(
403+
[
404+
"*'request' is a reserved name for fixtures and will raise an error in future versions"
405+
]
406+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def request():
6+
pass
7+
8+
9+
def test():
10+
pass

0 commit comments

Comments
 (0)