same blueprint cannot be registered with same name

This commit is contained in:
David Lord 2021-11-12 09:37:13 -08:00
parent f8cdc78ce1
commit 48f2afbf90
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 10 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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