Skip to content

Fixed signature of views decorated with signed_data_view #1657

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

Closed

Conversation

spookylukey
Copy link
Contributor

This fixes a nuisance when using django-debug-toolbar with django-urlconfchecks which type checks URLconfs, and possibly other tools. Otherwise it has little effect.

Basically, the @functools.wraps() call within the signed_data_view decorator makes views like sql_explain appear to have an external signature of sql_explain(request, verified_data) to tools like inspect.signature. If this was really the case, then the function would fail when used from the urlconf, because the verified_data parameter is not passed.

So, django-urlconfchecks raises an error for these views.

In reality signed_data_view is providing that argument, and so changing the effective signature of the function.

With the patch, the signature is changed to sql_explain(request, *args, **kwargs), which is not as accurate as it could be, but at least django-urlconfchecks now just issues a warning (about *args / **kwargs preventing signature checking), and not an error.

An alternative fix is to remove the @functools.wraps call entirely in the implementation of signed_data_view, but that would potentially produce more problems.

A test is added for this.

This removes a nuisance when using django-debug-toolbar with django-urlconfchecks
Copy link
Member

@matthiask matthiask left a comment

Choose a reason for hiding this comment

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

Thank you! Looks good to me. I checked the functools.update_wrapper implementation and I think this makes sense. I don't know much about typechecking though so I'll wait for a moment before merging.

@tim-schilling
Copy link
Member

I'm less sure about this. We're removing the attribute that's used to fetch the wrapped function. I can imagine that this will cause weirder issues than django-urlconfchecks down the line having a problem with this decorator pattern.

I'd prefer to see the debug toolbars url patterns excluded from evaluation or for us to rework this decorator so that it doesn't pass the verified data as an argument. Or pass it as a part of kwargs and hide it as an explicit keyword argument.

@tim-schilling
Copy link
Member

@spookylukey can you confirm that #1658 resolves the issue with django-urlconfchecks please?

@spookylukey
Copy link
Contributor Author

I did some research and found a package makefun to help create decorators that produce 100% correct signatures. With it, you would write something like:

@makefun.wraps(view, remove_args="verified_data") 

instead of @functools.wraps

https://smarie.github.io/python-makefun/#easier-edits

However, this adds a dependency and seems like overkill, unless django-debug-toolbar were itself adopting django-urlconfchecks for its own usage.

@tim-schilling
Copy link
Member

Ah, that's pretty neat but I'd prefer to avoid adding additional dependencies to the project.

@spookylukey
Copy link
Contributor Author

@spookylukey can you confirm that #1658 resolves the issue with django-urlconfchecks please?

Yes, I've checked and that works.

@tim-schilling
Copy link
Member

Closing in favor of #1658

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.

3 participants