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

injector._BindingNotYetAvailable: name 'Response' is not defined #66

Closed
jsr1337 opened this issue May 23, 2021 · 9 comments
Closed

injector._BindingNotYetAvailable: name 'Response' is not defined #66

jsr1337 opened this issue May 23, 2021 · 9 comments

Comments

@jsr1337
Copy link

jsr1337 commented May 23, 2021

Hello,
based on code from this post: #40 injector throws the above error. My code is almost identical, the only difference is flask_restful instead of flask_restplus:

from flask import Flask
from flask_restful import Api, Resource
from flask_injector import FlaskInjector, inject, singleton

app = Flask(name)
app.secret_key = "123"
api = Api(app=app)

class MyConf():
def init(self, val: int):
self.val = val

class MyApi(Resource):
@Inject
def init(self, conf: MyConf, **kwargs): # <- here just added **kwargs to receice the extra passed api parameter
self.val = conf.val
# Don't know if really needed
super().init(**kwargs)

def get(self, conf: MyConf):
    return {'x': conf.val}, 200

api.add_resource(MyApi, '/myapi')

def configure(binder):
binder.bind(
MyConf,
to=MyConf(456),
scope=singleton
)
# No need to bind the resource itself
#binder.bind(
# MyApi,
# to=MyApi(myConf)
#)

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

app.run(port=555, debug=True)

Am I doing something wrong here or this is a bug?
My setup:
Flask==2.0.1
Flask-HTTPAuth==4.4.0
Flask-Injector==0.12.3
Flask-JWT==0.3.2
Flask-RESTful==0.3.9
typing-extensions==3.7.4.3
typing-inspect==0.6.0
injector==0.18.4

@ghostman2013
Copy link

I have the same error (Python 3.9 x64, Flask v2.0.1, Flask-Injector v0.12.3)

@ghostman2013
Copy link

My code that worked on older versions of Flask-Injector:

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


class AppModule(Module):
    config: Dict[str, Any]

    def __init__(self, config: Dict[str, Any]):
        self.config = config

    def configure(self, binder: Binder):
        dao = Dao(db.session)
        app_path = PathService(self.config)
        notify = NotifyService(self.config)
        binder.bind(Dao, to=dao, scope=request)
        binder.bind(PathService, to=app_path, scope=request)
        binder.bind(NotifyService, to=notify, scope=request)

And base REST-resource:

class BaseResource(Resource):
    dao: Dao
    path: PathService

    @inject
    def __init__(self, dao: Dao, path: PathService) -> None:
        super(BaseResource, self).__init__()
        self.dao = dao
        self.path = path

What's wrong with this code? Unfortunately, debugging didn't help me. I found that the error happens in get_type_hints() function in injector module but I still can't get a reason of this error and how to fix it.

Error:

name 'Response' is not defined

@clementta
Copy link

Hi,
See #65

TL;DR : Issue with Flask > 2 using werkzeug > 2

@jstasiak
Copy link
Collaborator

Fixed in 0.13.0, thank you for the report.

@mallycrip
Copy link

Hello. Is this error fixed?

I have the same error

    raise _BindingNotYetAvailable(e)
injector._BindingNotYetAvailable: name 'Response' is not defined

Here is my environment

$ python --version
Python 3.9.5
$ pip freeze 
aniso8601==9.0.1
click==8.0.1
colorama==0.4.4
Flask==2.0.1
Flask-Injector==0.13.0
Flask-RESTful==0.3.9
injector==0.18.4
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
pytz==2021.1
six==1.16.0
Werkzeug==2.0.1
import flask_restful
from flask import Flask
from flask_injector import FlaskInjector
from injector import inject


class HelloWorld(flask_restful.Resource):
    @inject
    def __init__(self, *args, int: int, **kwargs):
        self._int = int
        super().__init__(*args, **kwargs)

    def get(self):
        return {'int': self._int}


app = Flask(__name__)
api = flask_restful.Api(app)

api.add_resource(HelloWorld, '/')

FlaskInjector(app=app)

app.run()

@afrischk
Copy link

afrischk commented Apr 8, 2022

Hello. Is this error fixed?

I have the same error

    raise _BindingNotYetAvailable(e)
injector._BindingNotYetAvailable: name 'Response' is not defined

Same here. I use

flask==2.1.1
flask-injector==0.13.0

I don't have Werkzeug as a dependency. Any advice?

@ghostman2013
Copy link

Hello. Is this error fixed?
I have the same error

    raise _BindingNotYetAvailable(e)
injector._BindingNotYetAvailable: name 'Response' is not defined

Same here. I use

flask==2.1.1
flask-injector==0.13.0

I don't have Werkzeug as a dependency. Any advice?

We have downgraded Flask version to <2.0.0, it helped. Unfortunately, it was a radical way that may not suit for you. I don’t work with Flask for few months already, so I don’t know if the situation was changed.

@afrischk
Copy link

afrischk commented Apr 8, 2022

We have downgraded Flask version to <2.0.0, it helped. Unfortunately, it was a radical way that may not suit for you. I don’t work with Flask for few months already, so I don’t know if the situation was changed.

Thanks for the reply. Unfortunately I don't think this will be an option for me.

@afrischk
Copy link

I worked around the issue by replacing the dependency injection container.

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

6 participants