Skip to content

Commit b8685f2

Browse files
committed
Address PR review feedback: update error message wording and example
- Change 'must be applied before' to 'must come after (below) the' to match DRF docs language - Fix decorator order in example to show @api_view first, then policy decorator below - Remove unnecessary f-string prefixes from non-interpolated lines - Update all test assertions to match new error message wording Addresses feedback from @browniebroke in PR #9821
1 parent 17a2d44 commit b8685f2

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

rest_framework/decorators.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def _check_decorator_order(func, decorator_name):
9494
# Check if func is actually a view function (result of APIView.as_view())
9595
if hasattr(func, 'cls') and issubclass(func.cls, APIView):
9696
raise TypeError(
97-
f"@{decorator_name} must be applied before @api_view. "
98-
f"The correct order is:\n\n"
99-
f" @api_view(['GET'])\n"
97+
f"@{decorator_name} must come after (below) the @api_view decorator. "
98+
"The correct order is:\n\n"
99+
" @api_view(['GET'])\n"
100100
f" @{decorator_name}(...)\n"
101-
f" def my_view(request):\n"
102-
f" ...\n\n"
103-
f"See https://www.django-rest-framework.org/api-guide/views/#api-policy-decorators"
101+
" def my_view(request):\n"
102+
" ...\n\n"
103+
"See https://www.django-rest-framework.org/api-guide/views/#api-policy-decorators"
104104
)
105105

106106

tests/test_decorators.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_incorrect_decorator_order_permission_classes(self):
214214
def view(request):
215215
return Response({})
216216

217-
assert '@permission_classes must be applied before @api_view' in str(cm.exception)
217+
assert '@permission_classes must come after (below) the @api_view decorator' in str(cm.exception)
218218

219219
def test_incorrect_decorator_order_renderer_classes(self):
220220
"""
@@ -226,7 +226,7 @@ def test_incorrect_decorator_order_renderer_classes(self):
226226
def view(request):
227227
return Response({})
228228

229-
assert '@renderer_classes must be applied before @api_view' in str(cm.exception)
229+
assert '@renderer_classes must come after (below) the @api_view decorator' in str(cm.exception)
230230

231231
def test_incorrect_decorator_order_parser_classes(self):
232232
"""
@@ -238,7 +238,7 @@ def test_incorrect_decorator_order_parser_classes(self):
238238
def view(request):
239239
return Response({})
240240

241-
assert '@parser_classes must be applied before @api_view' in str(cm.exception)
241+
assert '@parser_classes must come after (below) the @api_view decorator' in str(cm.exception)
242242

243243
def test_incorrect_decorator_order_authentication_classes(self):
244244
"""
@@ -250,7 +250,7 @@ def test_incorrect_decorator_order_authentication_classes(self):
250250
def view(request):
251251
return Response({})
252252

253-
assert '@authentication_classes must be applied before @api_view' in str(cm.exception)
253+
assert '@authentication_classes must come after (below) the @api_view decorator' in str(cm.exception)
254254

255255
def test_incorrect_decorator_order_throttle_classes(self):
256256
"""
@@ -265,7 +265,7 @@ class OncePerDayUserThrottle(UserRateThrottle):
265265
def view(request):
266266
return Response({})
267267

268-
assert '@throttle_classes must be applied before @api_view' in str(cm.exception)
268+
assert '@throttle_classes must come after (below) the @api_view decorator' in str(cm.exception)
269269

270270
def test_incorrect_decorator_order_versioning_class(self):
271271
"""
@@ -277,7 +277,7 @@ def test_incorrect_decorator_order_versioning_class(self):
277277
def view(request):
278278
return Response({})
279279

280-
assert '@versioning_class must be applied before @api_view' in str(cm.exception)
280+
assert '@versioning_class must come after (below) the @api_view decorator' in str(cm.exception)
281281

282282
def test_incorrect_decorator_order_metadata_class(self):
283283
"""
@@ -289,7 +289,7 @@ def test_incorrect_decorator_order_metadata_class(self):
289289
def view(request):
290290
return Response({})
291291

292-
assert '@metadata_class must be applied before @api_view' in str(cm.exception)
292+
assert '@metadata_class must come after (below) the @api_view decorator' in str(cm.exception)
293293

294294
def test_incorrect_decorator_order_content_negotiation_class(self):
295295
"""
@@ -305,7 +305,7 @@ def select_renderer(self, request, renderers, format_suffix):
305305
def view(request):
306306
return Response({})
307307

308-
assert '@content_negotiation_class must be applied before @api_view' in str(cm.exception)
308+
assert '@content_negotiation_class must come after (below) the @api_view decorator' in str(cm.exception)
309309

310310
def test_incorrect_decorator_order_schema(self):
311311
"""
@@ -320,7 +320,7 @@ class CustomSchema(AutoSchema):
320320
def view(request):
321321
return Response({})
322322

323-
assert '@schema must be applied before @api_view' in str(cm.exception)
323+
assert '@schema must come after (below) the @api_view decorator' in str(cm.exception)
324324

325325

326326
class ActionDecoratorTestCase(TestCase):

0 commit comments

Comments
 (0)