Skip to content

Conversation

@kernelshard
Copy link
Contributor

@kernelshard kernelshard commented Nov 6, 2025

Fixes #9596

This PR adds validation to detect when API policy decorators (@permission_classes, @renderer_classes, etc.) are applied in the wrong order with @api_view.

Problem

When policy decorators are applied after @api_view (i.e., placed above it in code), the policy settings are silently ignored. This happens because:

  • If policy decorators are applied after @api_view, they set attributes on the already-created view function, not the original function

Solution

Added a _check_decorator_order() helper that:

  • Detects when a decorator is applied to a view function (result of APIView.as_view())
  • Raises a clear TypeError with the correct decorator order
  • Applied to all 9 policy decorators

Tests

Added 9 comprehensive tests covering all policy decorators to ensure they properly validate ordering.

Raise TypeError when API policy decorators (@permission_classes,
@renderer_classes, etc.) are applied after @api_view instead of before it.

Fixes encode#9596
@auvipy auvipy requested a review from Copilot November 6, 2025 17:58
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds validation to ensure API policy decorators are applied in the correct order relative to the @api_view decorator. The feature prevents a common mistake where developers apply policy decorators after @api_view, which silently fails to work as expected.

  • Introduced _check_decorator_order() helper function to detect incorrect decorator ordering
  • Added order validation to all nine API policy decorators
  • Comprehensive test coverage for all affected decorators

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
rest_framework/decorators.py Added _check_decorator_order() validation function and integrated it into all API policy decorators to raise TypeError when applied after @api_view
tests/test_decorators.py Added nine test cases to verify that each API policy decorator raises TypeError when incorrectly applied after @api_view

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- 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 encode#9821
@auvipy auvipy added this to the 3.17 milestone Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Views decorators ordering

3 participants