From c2920e2bd98cdb3dcccc2868c25b695d4780c620 Mon Sep 17 00:00:00 2001 From: pgjones Date: Tue, 18 May 2021 13:37:11 +0100 Subject: [PATCH] Bugfix allow blueprints to be registered with a different name This allows the same blueprint to be registered multiple times at the same level, but with differing url_prefixes and names. --- src/flask/blueprints.py | 3 ++- tests/test_blueprints.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 8fe7d9e9..61b0ac69 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -274,7 +274,8 @@ class Blueprint(Scaffold): first_registration = False name_prefix = options.get("name_prefix", "") - name = f"{name_prefix}.{self.name}".lstrip(".") + self_name = options.get("name", self.name) + name = f"{name_prefix}.{self_name}".lstrip(".") if name in app.blueprints and app.blueprints[name] is not self: raise ValueError( diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 0f9e9db9..2a93e8fa 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -866,3 +866,16 @@ def test_nesting_url_prefixes( response = client.get("/parent/child/") assert response.status_code == 200 + + +def test_unique_blueprint_names(app, client) -> None: + bp = flask.Blueprint("bp", __name__) + bp2 = flask.Blueprint("bp", __name__) + + app.register_blueprint(bp) + app.register_blueprint(bp) # same name, same object, no error + + with pytest.raises(ValueError): + app.register_blueprint(bp2) # same name, different object + + app.register_blueprint(bp2, name="alt") # different name