flask_injector
flask_injector copied to clipboard
Does not work if class based view is decorated.
closure_contents = (c.cell_contents for c in cast(Any, fun).__closure__)
fun_closure = dict(zip(fun.__code__.co_freevars, closure_contents))
try:
class_kwargs = fun_closure['class_kwargs']
except KeyError:
# Most likely flask_restful resource, we'll see in a second
flask_restful_api = fun_closure['self']
# flask_restful wraps ResourceClass.as_view() result in its own wrapper
# the as_view() result is available under 'resource' name in this closure
fun = fun_closure['resource']
fun_closure = {}
class_kwargs = {}
# if the lines above succeeded we're quite sure it's flask_restful resource
The argument fun
is the view function. If it's decorated, the value of fun.code.co_freevars will be scoped to the decorator, and it won't include fun_closure['self']