0

I use FlaskInjector (0.14.0) and Flask==2.3.2

And when I do this setup in my app.py:

app = create_app()
FlaskInjector(app=app, modules=[configure])
app.run(port=8000, debug=app.config["DEVELOPMENT"])

I will get this error:

Traceback (most recent call last):
  File "Flask\venv\lib\site-packages\flask_injector\__init__.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 "C:\Program Files (x86)\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pydev\pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 2022.3.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "Flask\backend\app.py", line 246, in <module>
    FlaskInjector(app=app, modules=[configure])
  File "Flask\venv\lib\site-packages\flask_injector\__init__.py", line 318, in __init__
    process_dict(container, injector)
  File "Flask\venv\lib\site-packages\flask_injector\__init__.py", line 375, in process_dict
    d[key] = wrap_fun(value, injector)
  File "Flask\venv\lib\site-packages\flask_injector\__init__.py", line 63, in wrap_fun
    return wrap_class_based_view(fun, injector)
  File "Flask\venv\lib\site-packages\flask_injector\__init__.py", line 104, in wrap_class_based_view
    flask_restful_api = fun_closure['self']
KeyError: 'self'

But I don´t get why. This is how I created a configure method to register all my bindings that should be available inside my methods views (for API).

This is my "binding" method called from the app.py

    def configure(binder):
       binder.bind(HttpClient,to=HttpClient)

HttpClient is only demo test class, for testing my dependency injection if I can touch it in my views. I tried also to bind this class without inheriting from "Module"

from injector import Module

class HttpClient(Module):

    def test_method(self):
        print("haha iam test method")

Even If I don´t use any @inject for my class based views, I am not able to build the app itself. I followed the documentation for FlaskInjector.

As I mentioned it is even problem to build the flask app with this setup.

My goal was to create this sort of API endpoint:

from flask.views import MethodView

class IndexPageAPI(MethodView):
    """Servers basic statistics to the user once he loads index page."""
    init_every_request = False

    @inject
    def get(self, http_client: HttpClient):
        self.http_client.test_method()
        return jsonify({'data': 'Index page api call'})

But I didn´t get even to the point to try to access injected dependencies inside APi as I can´t build the app.

Does anyone see a problem, or what should be changed? Thank you very much.

Saurus
  • 13
  • 1
  • 3

1 Answers1

0

Based on the FlaskInjector open issues it seems that it has in general problem with class-based MethodViews. Probably so far the only solution is to use a different class from which you inherit.

Saurus
  • 13
  • 1
  • 3