Make 'request' a reserved name for fixtures
This commit is contained in:
		
							parent
							
								
									85cc12e328
								
							
						
					
					
						commit
						0ed7aa2db6
					
				|  | @ -19,6 +19,8 @@ removed: | ||||||
| 
 | 
 | ||||||
| * ``RemovedInPytest4Warning`` warning type. | * ``RemovedInPytest4Warning`` warning type. | ||||||
| 
 | 
 | ||||||
|  | * ``request`` is now a reserved name for fixtures. | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| For more information consult | For more information consult | ||||||
| `Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__ in the docs. | `Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__ in the docs. | ||||||
|  |  | ||||||
|  | @ -50,11 +50,11 @@ def iscoroutinefunction(func): | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def getlocation(function, curdir): | def getlocation(function, curdir=None): | ||||||
|     function = get_real_func(function) |     function = get_real_func(function) | ||||||
|     fn = py.path.local(inspect.getfile(function)) |     fn = py.path.local(inspect.getfile(function)) | ||||||
|     lineno = function.__code__.co_firstlineno |     lineno = function.__code__.co_firstlineno | ||||||
|     if fn.relto(curdir): |     if curdir is not None and fn.relto(curdir): | ||||||
|         fn = fn.relto(curdir) |         fn = fn.relto(curdir) | ||||||
|     return "%s:%d" % (fn, lineno + 1) |     return "%s:%d" % (fn, lineno + 1) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -18,6 +18,7 @@ from pluggy import PluginManager | ||||||
| 
 | 
 | ||||||
| import _pytest._code | import _pytest._code | ||||||
| import _pytest.assertion | import _pytest.assertion | ||||||
|  | import _pytest.deprecated | ||||||
| import _pytest.hookspec  # the extension point definitions | import _pytest.hookspec  # the extension point definitions | ||||||
| from .exceptions import PrintHelp | from .exceptions import PrintHelp | ||||||
| from .exceptions import UsageError | from .exceptions import UsageError | ||||||
|  |  | ||||||
|  | @ -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( | FUNCARGNAMES = PytestDeprecationWarning( | ||||||
|     "The `funcargnames` attribute was an alias for `fixturenames`, " |     "The `funcargnames` attribute was an alias for `fixturenames`, " | ||||||
|     "since pytest 2.3 - use the newer attribute instead." |     "since pytest 2.3 - use the newer attribute instead." | ||||||
|  |  | ||||||
|  | @ -2,7 +2,6 @@ import functools | ||||||
| import inspect | import inspect | ||||||
| import itertools | import itertools | ||||||
| import sys | import sys | ||||||
| import warnings |  | ||||||
| from collections import defaultdict | from collections import defaultdict | ||||||
| from collections import deque | from collections import deque | ||||||
| from collections import OrderedDict | from collections import OrderedDict | ||||||
|  | @ -26,7 +25,6 @@ from _pytest.compat import getlocation | ||||||
| from _pytest.compat import is_generator | from _pytest.compat import is_generator | ||||||
| from _pytest.compat import NOTSET | from _pytest.compat import NOTSET | ||||||
| from _pytest.compat import safe_getattr | from _pytest.compat import safe_getattr | ||||||
| from _pytest.deprecated import FIXTURE_NAMED_REQUEST |  | ||||||
| from _pytest.outcomes import fail | from _pytest.outcomes import fail | ||||||
| from _pytest.outcomes import TEST_OUTCOME | from _pytest.outcomes import TEST_OUTCOME | ||||||
| 
 | 
 | ||||||
|  | @ -971,7 +969,13 @@ class FixtureFunctionMarker: | ||||||
| 
 | 
 | ||||||
|         name = self.name or function.__name__ |         name = self.name or function.__name__ | ||||||
|         if name == "request": |         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 |         function._pytestfixturefunction = self | ||||||
|         return function |         return function | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,8 +1,6 @@ | ||||||
| import pytest | import pytest | ||||||
| from _pytest import deprecated | from _pytest import deprecated | ||||||
| 
 | 
 | ||||||
| pytestmark = pytest.mark.pytester_example_path("deprecated") |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| @pytest.mark.filterwarnings("default") | @pytest.mark.filterwarnings("default") | ||||||
| def test_resultlog_is_deprecated(testdir): | def test_resultlog_is_deprecated(testdir): | ||||||
|  | @ -46,13 +44,3 @@ def test_external_plugins_integrated(testdir, plugin): | ||||||
| 
 | 
 | ||||||
|     with pytest.warns(pytest.PytestConfigWarning): |     with pytest.warns(pytest.PytestConfigWarning): | ||||||
|         testdir.parseconfig("-p", plugin) |         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" |  | ||||||
|         ] |  | ||||||
|     ) |  | ||||||
|  |  | ||||||
|  | @ -1126,21 +1126,6 @@ class TestFixtureUsages: | ||||||
|         values = reprec.getfailedcollections() |         values = reprec.getfailedcollections() | ||||||
|         assert len(values) == 1 |         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): |     def test_usefixtures_marker(self, testdir): | ||||||
|         testdir.makepyfile( |         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[[]a[]]*"]) | ||||||
|     result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"]) |     result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"]) | ||||||
|     result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"]) |     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", | ||||||
|  |         ] | ||||||
|  |     ) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue