italomaia / flask-empty

An empty project skeleton / boilerplate for flask projects. Powered by CookieCutter.
662 stars 92 forks source link

blueprint already registered #44

Open inktrap opened 3 years ago

inktrap commented 3 years ago

I get this warning that will become an error:

UserWarning: The name 'foobar' is already registered for this blueprint. Use 'name=' to provide a unique name. This will become an error in Flask 2.1.
  self.register_blueprint(blueprint, **kw)

I checked out the source and in main.py app_factory() calls app.add_blueprint_list(). However, flask-security also tries to register all blueprints. Since flask's add_blueprint() method doesn't check wether a blueprint is already registered or not, it is attempted to register the same blueprints twice.

You can not unregister a blueprint:

The downside is that you cannot unregister a blueprint once an application was created without having to destroy the whole application object.

Who should fix this? add_blueprint is a part of flask … should every caller implement their own logic to check if a blueprint was already registered?

iamhazel commented 2 years ago

Ugh I didn't realize this could happen, that's frustrating. How would you even "destroy the whole application object"?

inktrap commented 2 years ago

Obviously with app.destroy()

No just kidding, but there is a teardown_appcontext() which "Registers a function to be called when the application context ends. These functions are typically also called when the request context is popped."

But destroying the app-context wouldn't be the solution to this anyways … so I don't know if we are overlooking something more fundamental? Maybe we can ask for help?

So funny when old GitHub issues pop up and your issue-context got destroyed and you have to recreate it again ;P glad you replied :)

iamhazel commented 2 years ago

Obviously with app.destroy()

No just kidding, but there is a teardown_appcontext() which "Registers a function to be called when the application context ends. These functions are typically also called when the request context is popped."

But destroying the app-context wouldn't be the solution to this anyways … so I don't know if we are overlooking something more fundamental? Maybe we can ask for help?

So funny when old GitHub issues pop up and your issue-context got destroyed and you have to recreate it again ;P glad you replied :)

This is all a bit above my head tbh. I just want to name my blueprints what I want 😂 I didn't realize anything is "persistent" when you stop and re-run the application.

inktrap commented 2 years ago

No, this is not persistent from run to run. I guess it just says that if a blueprint is registered, you will have to destroy the app object and there is no unregister function. And yes, you are free to name your blueprints however you like, but the name has to be unique. Isn't this namespaced anyways?

I am a bit out of the loop regarding this issue and will have to dig more into flask (which I am currently not even using) and maybe ask for help. So I am just re-iterating my first comment on this: the issue is that add_blueprint_list() is called twice (by flask-security and by flask-empty) and this attempts to register the (already registered) blueprints again, but obviously, blueprint names have to be unique.

I guess we are overlooking something relatively simple here and it seems improbable that nobody ran into this issue before or that is indeed a bug in flask.

italomaia commented 2 years ago

Can you show a minimal example? add_blueprint_list is not called by flask-security.

jbhanks commented 2 years ago

I seem to be having this problem too. I was getting this error on some nested blueprints (which in my code are only explicitly registered once). I came across this and removed flask_login, and that stopped the error. I'm still not sure what is going on exactly.