Skip to content
Myr

warn when registering same blueprint with same name

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by pgjones · parent 63b3067 · Browse files · 3 files+34 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+34 added−17 removed

  • tests:+8 added−4 removed
  • other:+26 added−13 removed

3 files+34 added−17 removed

modified:src/flask/blueprints.py+24 added-12 removed
281 unmodified lines
282 The ``name`` option can be used to change the (pre-dotted)283 name the blueprint is registered with. This allows the same284 blueprint to be registered multiple times with unique names285 for ``url_for``.286 """287 first_registration = True286289 for blueprint in app.blueprints.values():290 if blueprint is self:291 first_registration = False292287 .. versionchanged:: 2.0.1288 Registering the same blueprint with the same name multiple289 times is deprecated and will become an error in Flask 2.1.290 """291 first_registration = not any(bp is self for bp in app.blueprints.values())292 name_prefix = options.get("name_prefix", "")293 self_name = options.get("name", self.name)294 name = f"{name_prefix}.{self_name}".lstrip(".")295297 if name in app.blueprints and app.blueprints[name] is not self:298 raise ValueError(299 f"Blueprint name '{self.name}' "300 f"is already registered by {app.blueprints[self.name]}. "301 "Blueprints must have unique names."302 )296 if name in app.blueprints:297 existing_at = f" '{name}'" if self_name != name else ""298299 if app.blueprints[name] is not self:300 raise ValueError(301 f"The name '{self_name}' is already registered for"302 f" a different blueprint{existing_at}. Use 'name='"303 " to provide a unique name."304 )305 else:306 import warnings307308 warnings.warn(309 f"The name '{self_name}' is already registered for"310 f" this blueprint{existing_at}. Use 'name=' to"311 " provide a unique name. This will become an error"312 " in Flask 2.1.",313 stacklevel=4,314 )315316 app.blueprints[name] = self317 self._got_registered_once = True318 state = self.make_setup_state(app, options, first_registration)319
280 unmodified lines
modified:tests/test_blueprints.py+8 added-4 removed
138 unmodified lines
139 def bar(bar):140 return str(bar)141142 app.register_blueprint(bp, url_prefix="/1", url_defaults={"bar": 23})143 app.register_blueprint(bp, url_prefix="/2", url_defaults={"bar": 19})143 app.register_blueprint(bp, name="test2", url_prefix="/2", url_defaults={"bar": 19})144145 assert client.get("/1/foo").data == b"23/42"146 assert client.get("/2/foo").data == b"19/42"147 assert client.get("/1/bar").data == b"23"
724 unmodified lines
872 bp = flask.Blueprint("bp", __name__)873 bp2 = flask.Blueprint("bp", __name__)874875 app.register_blueprint(bp)876 app.register_blueprint(bp) # same name, same object, no error876877 with pytest.warns(UserWarning):878 app.register_blueprint(bp) # same bp, same name, warning879880 app.register_blueprint(bp, name="again") # same bp, different name, ok881882 with pytest.raises(ValueError):879 app.register_blueprint(bp2) # same name, different object883 app.register_blueprint(bp2) # different bp, same name, error884881 app.register_blueprint(bp2, name="alt") # different name885 app.register_blueprint(bp2, name="alt") # different bp, different name, ok
modified:CHANGES.rst+2 added-1 removed
31 unmodified lines
32 locations. :issue:`4069`33- ``register_blueprint`` takes a ``name`` option to change the34 (pre-dotted) name the blueprint is registered with. This allows the35 same blueprint to be registered multiple times with unique names for36 ``url_for``. :issue:`1091`36 ``url_for``. Registering the same blueprint with the same name37 multiple times is deprecated. :issue:`1091`383940Version 2.0.041-------------
1116 unmodified lines