Skip to content

Error with flask method view #38

Open
@JacksonToomey

Description

@JacksonToomey

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions