Skip to content

Commit

Permalink
Revert "Make api_view respect standard wrapper assignments (#8291)" (#…
Browse files Browse the repository at this point in the history
…8297)

This reverts commit 9c97946.
  • Loading branch information
tomchristie committed Dec 15, 2021
1 parent 2d52c9e commit a780e80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 16 additions & 4 deletions rest_framework/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
methods on viewsets that should be included by routers.
"""
import types
from functools import update_wrapper

from django.forms.utils import pretty_name

Expand All @@ -23,8 +22,18 @@ def api_view(http_method_names=None):

def decorator(func):

class WrappedAPIView(APIView):
pass
WrappedAPIView = type(
'WrappedAPIView',
(APIView,),
{'__doc__': func.__doc__}
)

# Note, the above allows us to set the docstring.
# It is the equivalent of:
#
# class WrappedAPIView(APIView):
# pass
# WrappedAPIView.__doc__ = func.doc <--- Not possible to do this

# api_view applied without (method_names)
assert not(isinstance(http_method_names, types.FunctionType)), \
Expand All @@ -43,6 +52,9 @@ def handler(self, *args, **kwargs):
for method in http_method_names:
setattr(WrappedAPIView, method.lower(), handler)

WrappedAPIView.__name__ = func.__name__
WrappedAPIView.__module__ = func.__module__

WrappedAPIView.renderer_classes = getattr(func, 'renderer_classes',
APIView.renderer_classes)

Expand All @@ -61,7 +73,7 @@ def handler(self, *args, **kwargs):
WrappedAPIView.schema = getattr(func, 'schema',
APIView.schema)

return update_wrapper(WrappedAPIView.as_view(), func)
return WrappedAPIView.as_view()

return decorator

Expand Down
10 changes: 0 additions & 10 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,6 @@ def view(request):

assert isinstance(view.cls.schema, CustomSchema)

def test_wrapper_assignments(self):
@api_view(["GET"])
def test_view(request):
"""example docstring"""
pass

assert test_view.__name__ == "test_view"
assert test_view.__doc__ == "example docstring"
assert test_view.__qualname__ == "DecoratorTestCase.test_wrapper_assignments.<locals>.test_view"


class ActionDecoratorTestCase(TestCase):

Expand Down

0 comments on commit a780e80

Please sign in to comment.