Skip to content
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

Test of failing functionality #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions flask_injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
return wrapper # type: ignore


def _resolve_wraped_function(func: Callable) -> Callable:
"""
Decorators that use functools wraps will not have
a func.__code__ object that matches what View.as_view
creates breaking code.

This gets the original undecorated function.
"""
if hasattr(func, '__wrapped__'):
return _resolve_wraped_function(func.__wrapped__)
else:
return func


def wrap_fun(fun: T, injector: Injector) -> T:
if isinstance(fun, LocalProxy):
return fun # type: ignore
Expand Down Expand Up @@ -101,6 +115,7 @@ def wrap_class_based_view(fun: Callable, injector: Injector) -> Callable:
cls = cast(Any, fun).view_class
name = fun.__name__

fun = _resolve_wraped_function(fun)
closure_contents = (c.cell_contents for c in cast(Any, fun).__closure__)
fun_closure = dict(zip(fun.__code__.co_freevars, closure_contents))
try:
Expand Down
32 changes: 31 additions & 1 deletion flask_injector/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gc
import json
import warnings
from functools import partial
from functools import partial, wraps
from typing import NewType

import flask_restful
Expand Down Expand Up @@ -284,6 +284,36 @@ def dispatch_request(self, dispatch_arg):
eq_(response.data, b'aaa bbb')


def test_view_decoration_works():

def decorator(view):
@wraps(view)
def wrapper(*args, **kwargs):
return view(*args, **kwargs)

return wrapper

class MyView(View):
decorators = [decorator]

def __init__(self, class_arg):
self.class_arg = class_arg

def dispatch_request(self, dispatch_arg):
return '%s %s' % (self.class_arg, dispatch_arg)


app = Flask(__name__)
app.add_url_rule('/<dispatch_arg>', view_func=MyView.as_view('view', class_arg='aaa'))

FlaskInjector(app=app)

client = app.test_client()
response = client.get('/bbb')
print(response.data)
eq_(response.data, b'aaa bbb')


def test_flask_restful_integration_works():
class HelloWorld(flask_restful.Resource):
@inject
Expand Down