matipau · GitHub

When using nested blueprints, handlers registered via url_value_preprocessor, before_request, context_processor, or url_defaults are called in an inconsistent order: First on the app, then on the deepest blueprint, and then upwards through all parents (except the app). Here is a minimal example:

from flask import Flask, Blueprint, g
app = Flask(__name__)
parent = Blueprint('parent', __name__)
child = Blueprint('child', __name__)
@app.before_request
def app_before():
    g.test = 'app'
@parent.before_request
def parent_before():
    g.test = 'parent'
@child.before_request
def child_before():
    g.test = 'child'
@child.route('/')
def test():
    return g.test
parent.register_blueprint(child)
app.register_blueprint(parent)

I would expect this to return child, but it returns parent.

Environment:

  • Python version: 3.9.6
  • Flask version: 2.0.1

Read the original on github.com ↗