flask_injector icon indicating copy to clipboard operation
flask_injector copied to clipboard

Error with flask method view

Open JacksonToomey opened this issue 5 years ago • 2 comments

When using flask's own MethodView with flask injector, the app fails with the error:

Traceback (most recent call last):
  File "/Users/jacksontoomey1/.virtualenvs/rooster-d4LzYqiE/lib/python3.7/site-packages/flask_injector.py", line 101, in wrap_class_based_view
    class_kwargs = fun_closure['class_kwargs']
KeyError: 'class_kwargs'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example.py", line 28, in <module>
    FlaskInjector(app=app, modules=[])
  File "/Users/jacksontoomey1/.virtualenvs/rooster-d4LzYqiE/lib/python3.7/site-packages/flask_injector.py", line 317, in __init__
    process_dict(container, injector)
  File "/Users/jacksontoomey1/.virtualenvs/rooster-d4LzYqiE/lib/python3.7/site-packages/flask_injector.py", line 344, in process_dict
    d[key] = wrap_fun(value, injector)
  File "/Users/jacksontoomey1/.virtualenvs/rooster-d4LzYqiE/lib/python3.7/site-packages/flask_injector.py", line 81, in wrap_fun
    return wrap_class_based_view(fun, injector)
  File "/Users/jacksontoomey1/.virtualenvs/rooster-d4LzYqiE/lib/python3.7/site-packages/flask_injector.py", line 104, in wrap_class_based_view
    flask_restful_api = fun_closure['self']
KeyError: 'self'

Here is an example that fails.

from functools import wraps

from flask import Flask
from flask.views import MethodView
from flask_injector import FlaskInjector


app = Flask(__name__)


def my_wrapper(f):
    @wraps(f)
    def _wrapper(*args, **kwargs):
        return f(*args, **kwargs)
    return _wrapper


class MyView(MethodView):
    decorators = [my_wrapper]

    def get(self):
        return 'Hello'


app.add_url_rule('/', view_func=MyView.as_view('index'))


FlaskInjector(app=app, modules=[])


if __name__ == '__main__':
    app.run()

JacksonToomey avatar Jul 07 '19 19:07 JacksonToomey

Is there any update on this?

JacksonToomey avatar Feb 06 '20 20:02 JacksonToomey

I meant to have a look at this some time ago but I forgot and I don't have time now. Pull requests that fix this (if it's even possible) are welcome though.

jstasiak avatar Feb 12 '20 12:02 jstasiak