-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Check extra action func.__name__ #7098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
28d7812
to
b789106
Compare
b789106
to
fe22df3
Compare
So, fixing this isn't really possible given how method mapping works. The method map that's passed to the router maps HTTP methods to viewset method names. However, if an intermediate decorator returns a new function that doesn't use def decorate(fn):
# doesn't use @functools.wraps(fn)
def wrapper(self, request, *args, **kwargs):
return fn(self, request, *args, **kwargs)
return wrapper
class MyViewSet(viewsets.ViewSet):
@action(detail=False)
@decorate
def extra(self, request, *args, **kwargs):
pass
@action.delete
@decorate
def extra_delete(self, request, *args, **kwargs):
pass What the router should be passed is The best we can do is warn the user after the fact. If the attribute name doesn't match the function name, then we can raise an error. |
fe22df3
to
2373439
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we drop python 3.5?
Not unless we drop Django 2.2 LTS, which still supports it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a conflict that needs resolving, but otherwise leaving this in your hands @rpkilby!
Thanks as ever!!! 😃
a3cc8a5
to
b06c8ea
Compare
The extra
@action
rework is incompatible with some decorator patterns. For example:The issue is that the
@action
rework looks up functions by their__name__
instead of their actual attribute name on the viewset. Normally, this isn't an issue since the two coincide. In the mean time, the workaround is to usefunctools.wraps
, which will correctly set the__name__
.Fixes #7097