diff --git a/rest_framework/request.py b/rest_framework/request.py index 7e4daf274..230e7deaf 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -11,6 +11,7 @@ The wrapped request then offers a richer API, in particular : from __future__ import unicode_literals import sys +import warnings from contextlib import contextmanager from django.conf import settings @@ -159,7 +160,7 @@ class Request(object): .format(request.__class__.__module__, request.__class__.__name__) ) - self._request = request + self.http_request = request self.parsers = parsers or () self.authenticators = authenticators or () self.negotiator = negotiator or self._default_negotiator() @@ -181,6 +182,22 @@ class Request(object): forced_auth = ForcedAuthentication(force_user, force_token) self.authenticators = (forced_auth,) + @property + def _request(self): + warnings.warn( + "`_request` has been deprecated in favor of `http_request`, and will be removed in 3.10", + PendingDeprecationWarning, stacklevel=2 + ) + return self.http_request + + @_request.setter + def _request(self, value): + warnings.warn( + "`_request` has been deprecated in favor of `http_request`, and will be removed in 3.10", + PendingDeprecationWarning, stacklevel=2 + ) + self.http_request = value + def _default_negotiator(self): return api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS() diff --git a/tests/test_request.py b/tests/test_request.py index 632f4a85e..36ef6676c 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -292,3 +292,22 @@ class TestHttpRequest(TestCase): message = "'Request' object has no attribute 'inner_property'" with self.assertRaisesMessage(AttributeError, message): request.inner_property + + def test_request_deprecation(self): + with pytest.warns(PendingDeprecationWarning) as record: + Request(factory.get('/'))._request + + assert len(record) == 1 + assert str(record[0].message) == ( + "`_request` has been deprecated in favor of " + "`http_request`, and will be removed in 3.10" + ) + + with pytest.warns(PendingDeprecationWarning) as record: + Request(factory.get('/'))._request = None + + assert len(record) == 1 + assert str(record[0].message) == ( + "`_request` has been deprecated in favor of " + "`http_request`, and will be removed in 3.10" + )