forked from orbit-oss/flask
same blueprint cannot be registered with same name
This commit is contained in:
parent
f8cdc78ce1
commit
48f2afbf90
3 changed files with 10 additions and 18 deletions
|
|
@ -17,6 +17,8 @@ Unreleased
|
||||||
instead.
|
instead.
|
||||||
- ``total_seconds`` is removed, use ``timedelta.total_seconds``
|
- ``total_seconds`` is removed, use ``timedelta.total_seconds``
|
||||||
instead.
|
instead.
|
||||||
|
- The same blueprint cannot be registered with the same name. Use
|
||||||
|
``name=`` when registering to specify a unique name.
|
||||||
|
|
||||||
|
|
||||||
Version 2.0.2
|
Version 2.0.2
|
||||||
|
|
|
||||||
|
|
@ -299,24 +299,14 @@ class Blueprint(Scaffold):
|
||||||
name = f"{name_prefix}.{self_name}".lstrip(".")
|
name = f"{name_prefix}.{self_name}".lstrip(".")
|
||||||
|
|
||||||
if name in app.blueprints:
|
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 ""
|
existing_at = f" '{name}'" if self_name != name else ""
|
||||||
|
|
||||||
if app.blueprints[name] is not self:
|
raise ValueError(
|
||||||
raise ValueError(
|
f"The name '{self_name}' is already registered for"
|
||||||
f"The name '{self_name}' is already registered for"
|
f" {bp_desc} blueprint{existing_at}. Use 'name=' to"
|
||||||
f" a different blueprint{existing_at}. Use 'name='"
|
f" provide a unique 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,
|
|
||||||
)
|
|
||||||
|
|
||||||
first_bp_registration = not any(bp is self for bp in app.blueprints.values())
|
first_bp_registration = not any(bp is self for bp in app.blueprints.values())
|
||||||
first_name_registration = name not in app.blueprints
|
first_name_registration = name not in app.blueprints
|
||||||
|
|
|
||||||
|
|
@ -954,8 +954,8 @@ def test_unique_blueprint_names(app, client) -> None:
|
||||||
|
|
||||||
app.register_blueprint(bp)
|
app.register_blueprint(bp)
|
||||||
|
|
||||||
with pytest.warns(UserWarning):
|
with pytest.raises(ValueError):
|
||||||
app.register_blueprint(bp) # same bp, same name, warning
|
app.register_blueprint(bp) # same bp, same name, error
|
||||||
|
|
||||||
app.register_blueprint(bp, name="again") # same bp, different name, ok
|
app.register_blueprint(bp, name="again") # same bp, different name, ok
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue