-
-
Notifications
You must be signed in to change notification settings - Fork 16k
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
Nested blueprint registration causes error when using test-scoped app fixtures #4786
Comments
What should be the expected behavior? Causes a warning (will soon be an error) looks like correct behavior. Do you think that there should be a method that makes these operations: parent._blueprints = []
parent._got_registered_once = False Maybe something like: parent.reset_blueprint() Or any another suggestion? Thanks. |
Hi |
I've replied to you on Discord. Please limit discussion here to the issue itself and ask side questions there. |
AssertionError: The setup method 'register_blueprint' can no longer be called on the application. pallets/flask#4786
AssertionError: The setup method 'register_blueprint' can no longer be called on the application. pallets/flask#4786
AssertionError: The setup method 'register_blueprint' can no longer be called on the application. pallets/flask#4786
The problem can be solve with a single line change which is causing the issue
The highlighted part is causing the issue since we are defining parent blueprint instance as global and registering child blueprint inside function so when To solve the problem we simple have to move the child blueprint registration to parent outside
|
I think the problem here is illustrated by @ChandanChainani's comment/solution. Blueprints are global objects, you're mutating the same object each time you call If the blueprints are always registered in the same way, you should move them outside the factory. If they change during setup, then maybe you want to create the "top" blueprint each time, like the Flask instance, then register the child blueprints before registering it on the app. |
For apps that use nested blueprints, the way that Flask currently tracks how child blueprints have been registered causes both an error and a warning (that will soon be an error) when creating multiple app objects. This is a problem when testing.
For the below code,
test_2
will fail becausechild
has already been registered onparent
, resulting inAdditionally, the second call to
parent.register_blueprint()
emits a warning:User workarounds:
FlaskLoginClient
from Flask-Login (but I worked around that too)create_app()
— this isn't ideal because it makes registering routes on the blueprints more complexyield
statement also fixes the problem for a given parent blueprint but is not a very flexible solution overall:Environment:
The text was updated successfully, but these errors were encountered: