From 48f2afbf900560af061d50c73e4d3f9422ce4dbe Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 12 Nov 2021 09:37:13 -0800 Subject: [PATCH] same blueprint cannot be registered with same name --- CHANGES.rst | 2 ++ src/flask/blueprints.py | 22 ++++++---------------- tests/test_blueprints.py | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3560dc65..4d6fa62d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -17,6 +17,8 @@ Unreleased instead. - ``total_seconds`` is removed, use ``timedelta.total_seconds`` instead. + - The same blueprint cannot be registered with the same name. Use + ``name=`` when registering to specify a unique name. Version 2.0.2 diff --git a/src/flask/blueprints.py b/src/flask/blueprints.py index 5c23a735..47465ad7 100644 --- a/src/flask/blueprints.py +++ b/src/flask/blueprints.py @@ -299,24 +299,14 @@ class Blueprint(Scaffold): name = f"{name_prefix}.{self_name}".lstrip(".") if name in app.blueprints: + bp_desc = "this" if app.blueprints[name] is self else "a different" existing_at = f" '{name}'" if self_name != name else "" - if app.blueprints[name] is not self: - raise ValueError( - f"The name '{self_name}' is already registered for" - f" a different blueprint{existing_at}. Use 'name='" - " to provide a unique name." - ) - else: - import warnings - - warnings.warn( - f"The name '{self_name}' is already registered for" - f" this blueprint{existing_at}. Use 'name=' to" - " provide a unique name. This will become an error" - " in Flask 2.1.", - stacklevel=4, - ) + raise ValueError( + f"The name '{self_name}' is already registered for" + f" {bp_desc} blueprint{existing_at}. Use 'name=' to" + f" provide a unique name." + ) first_bp_registration = not any(bp is self for bp in app.blueprints.values()) first_name_registration = name not in app.blueprints diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index e02cd4be..fbe9eeee 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -954,8 +954,8 @@ def test_unique_blueprint_names(app, client) -> None: app.register_blueprint(bp) - with pytest.warns(UserWarning): - app.register_blueprint(bp) # same bp, same name, warning + with pytest.raises(ValueError): + app.register_blueprint(bp) # same bp, same name, error app.register_blueprint(bp, name="again") # same bp, different name, ok