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

program fall on FlaskInjector(app=app, injector=injector) #65

Closed
mrKAndrew opened this issue May 18, 2021 · 23 comments
Closed

program fall on FlaskInjector(app=app, injector=injector) #65

mrKAndrew opened this issue May 18, 2021 · 23 comments

Comments

@mrKAndrew
Copy link

I have Flask app with flask-injector with creat function.

def create_app(testing: bool = False) -> Flask: app = Flask(__name__) config_app(app, testing) db.init_app(app) migrate.init_app(app, db) router.init_app(app) CORS(app) # Setup injector injector = Injector([AppModule(app.config)]) FlaskInjector(app=app, injector=injector) return app
when i start app (flask run) i get error:

` Traceback (most recent call last):
File "C:\Users\tmv28\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\tmv28\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "C:\Users\tmv28\Documents\Andrey\test\backend\venv\Scripts\flask.exe_main
.py", line 7, in
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\core.py", line 1059, in main
rv = self.invoke(ctx)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\core.py", line 1665, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\core.py", line 1401, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\core.py", line 767, in invoke
return __callback(*args, **kwargs)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\decorators.py", line 84, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\click\core.py", line 767, in invoke
return __callback(*args, **kwargs)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 848, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 305, in init
self.load_unlocked()
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 330, in load_unlocked
self.app = rv = self.loader()
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
import(module_name)
File "C:\Users\tmv28\Documents\Andrey\test\backend\main\server.py", line 4, in
app = create_instance()
File "C:\Users\tmv28\Documents\Andrey\test\backend\main\apps\server_init
.py", line 22, in create_instance
return create_app(kwargs.get('testing', False))
File "C:\Users\tmv28\Documents\Andrey\test\backend\main\apps\server\factory.py", line 41, in create_app
FlaskInjector(app=app, injector=injector)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask_injector_init
.py", line 327, in init
process_dict(container, injector)
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask_injector_init
.py", line 379, in process_dict
elif hasattr(value, 'call'):
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\werkzeug\local.py", line 422, in get
obj = instance._get_current_object()
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\werkzeug\local.py", line 544, in _get_current_object
return self.__local() # type: ignore
File "c:\users\tmv28\documents\andrey\test\backend\venv\lib\site-packages\flask\globals.py", line 38, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
`
what did i wrong?

@lacer93
Copy link

lacer93 commented May 19, 2021

Something similar also happens for me with Flask==2.0.0.

@clementta
Copy link

This commit fixes it: b57e5e0

It is fixed in master branch but not released yet.
Solution would be to have a 0.12.4 release

@ansonnn07
Copy link

I’m having the same issue when using FlaskInjector. Had to downgrade both Flask and Werkzeug versions to below 2.0 to make it work.

@evgenyk-storexray
Copy link

@clementta-tagheuer, huge thanks for fixing the issue.
Do you know when 0.12.4 will be released ?
It is a real blocker, since I'm Flask 2.x.x for async support and Injector doesn't supports new Flask.

@clementta
Copy link

Hi @evgenyk-storexray, unfortunately I can't do releases for this project.
We need either @alecthomas or @jstasiak to do it.

Meanwhile, I suggest you force Flask to 1.1.4 in your project, or drop Flask Injector dependance.

@EvgenyKhaliper
Copy link

Thanks, @clementta-tagheuer hopefully, one of them will be here soon. lol

@benomalley25
Copy link

@alecthomas , @jstasiak any updates on when the above bugfix could be released?

@heydev
Copy link

heydev commented Jul 8, 2021

Same, blocker for us since we won't downgrade Flask.

@jstasiak
Copy link
Collaborator

jstasiak commented Jul 9, 2021

This project doesn't work with Flask 2 yet. I'm afraid there'll be no releases until #66 is resolved (and I have no time to work on it).

@mic4ael
Copy link

mic4ael commented Jul 14, 2021

Any update on this?

@remirab
Copy link

remirab commented Jul 31, 2021

@alecthomas , @jstasiak please release the latest version. It's a real blocker.

@jstasiak
Copy link
Collaborator

I don't think there's anything to release, but please show a minimal, complete piece of code (together with the versions of Python, Flask, Werkzeug and any other relevant packages) that fails on the last released version but works with the master branch and I'll reconsider.

@gandalftheFFFFFF
Copy link

I don't think there's anything to release, but please show a minimal, complete piece of code (together with the versions of Python, Flask, Werkzeug and any other relevant packages) that fails on the last released version but works with the master branch and I'll reconsider.

Hi! I just hit this issue, but only on Windows. It works fine on macOS for me. Here is a very small example that reproduces the issue on my end:

requirements.txt:

flask==2.0.0
flask-injector==0.12.3

main.py

from flask import Flask
from flask_injector import FlaskInjector
from injector import singleton


class Dep:
    def __init__(self):
        pass


class Server:
    def __init__(self):
        app = Flask(__name__)

        @app.route('/')
        def index():
            return 'hello, world'

        def configure(binder):
            binder.bind(Dep, to=Dep(), scope=singleton)

        FlaskInjector(app=app, modules=[configure])

        self.app = app


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

@jstasiak
Copy link
Collaborator

@nielspedersen Thanks, two bits missing still: what Python and Werkzeug versions?

@gandalftheFFFFFF
Copy link

gandalftheFFFFFF commented Aug 23, 2021

@nielspedersen Thanks, two bits missing still: what Python and Werkzeug versions?

@jstasiak

Sorry for the missing details - I missed that.

$ python --version
Python 3.7.9
$ pip freeze
click==8.0.1
colorama==0.4.4
Flask==2.0.0
Flask-Injector==0.12.3
importlib-metadata==4.6.4
injector==0.18.4
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
typing-extensions==3.10.0.0
Werkzeug==2.0.1
zipp==3.5.0

So python 3.7.9 and Werkzeug 2.0.1

@jstasiak
Copy link
Collaborator

@nielspedersen Thank you for the update, all the information needed is there and I can reproduce the issue. I'll release something shortly.

@jstasiak
Copy link
Collaborator

This is fixed in 0.13.0. Thank you everyone for the information.

@valentk777
Copy link

valentk777 commented Nov 20, 2021

I still get this in Flask-Injector==0.13.0

env:

Flask-Cors==3.0.10
injector==0.18.4
Flask-Restful==0.3.9
Flask-Injector==0.13.0
def create_app(modules=[]) -> Flask:
      app = Flask(__name__)
      api = Api(app)

      api.add_resource(HealthResource, '/health', '/api/v1/health')

      FlaskInjector(app=app, modules=modules)
      return app
class HealthResource(Resource):
      def get(self):
            logger.info("Health request")
            return {
                'status': 'OK',
            }

@jstasiak
Copy link
Collaborator

Please provide the Flask and Python versions you're using and what error exactly are you getting (with full stack trace)

@valentk777
Copy link

Python 3.9
Flask 2.0.2

127.0.0.1 - - [20/Nov/2021 13:11:26] "GET /health HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\injector\__init__.py", line 1176, in _infer_injected_bindings
    bindings = get_type_hints(callable, include_extras=True)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\typing.py", line 1456, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\typing.py", line 292, in _eval_type
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\typing.py", line 292, in <genexpr>
    ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\typing.py", line 290, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\typing.py", line 546, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
    import typing as t
NameError: name 'Response' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_restful\__init__.py", line 271, in error_router
    return original_handler(e)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_restful\__init__.py", line 271, in error_router
    return original_handler(e)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_injector\__init__.py", line 89, in wrapper
    return injector.call_with_injection(callable=fun, args=args, kwargs=kwargs)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\injector\__init__.py", line 1010, in call_with_injection
    bindings = get_bindings(callable)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\injector\__init__.py", line 1163, in get_bindings
    callable, _infer_injected_bindings(callable, only_explicit_bindings=look_for_explicit_bindings)
  File "C:\Users\valen\AppData\Local\Programs\Python\Python39\Lib\site-packages\injector\__init__.py", line 1178, in _infer_injected_bindings
    raise _BindingNotYetAvailable(e)
injector._BindingNotYetAvailable: name 'Response' is not defined

@valentk777
Copy link

I will move to flask 1.1.2 to temporarily avoid the issue

@rshah88
Copy link

rshah88 commented Apr 19, 2022

Any update on this issue ? still reproducing on 0.13.0...not sure how it got closed.

@jstasiak
Copy link
Collaborator

Resolved in Flask-Injector 0.14.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests