Skip to content
Myr

same blueprint cannot be registered 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 David Lord · parent f8cdc78 · Browse files · 3 files+10 added−18 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+10 added−18 removed

  • tests:+2 added−2 removed
  • other:+8 added−16 removed

3 files+10 added−18 removed

modified:src/flask/blueprints.py+6 added-16 removed
297 unmodified lines
298 self_name = options.get("name", self.name)299 name = f"{name_prefix}.{self_name}".lstrip(".")300301 if name in app.blueprints:302 bp_desc = "this" if app.blueprints[name] is self else "a different"303 existing_at = f" '{name}'" if self_name != name else ""304304 if app.blueprints[name] is not self:305 raise ValueError(306 f"The name '{self_name}' is already registered for"307 f" a different blueprint{existing_at}. Use 'name='"308 " to provide a unique name."309 )305 raise ValueError(306 f"The name '{self_name}' is already registered for"307 f" {bp_desc} blueprint{existing_at}. Use 'name=' to"308 f" provide a unique name."309 )310 else:311 import warnings310313 warnings.warn(314 f"The name '{self_name}' is already registered for"315 f" this blueprint{existing_at}. Use 'name=' to"316 " provide a unique name. This will become an error"317 " in Flask 2.1.",318 stacklevel=4,319 )320311 first_bp_registration = not any(bp is self for bp in app.blueprints.values())312 first_name_registration = name not in app.blueprints313314 app.blueprints[name] = self
285 unmodified lines
modified:tests/test_blueprints.py+2 added-2 removed
952 unmodified lines
953 bp2 = flask.Blueprint("bp", __name__)954955 app.register_blueprint(bp)956957 with pytest.warns(UserWarning):958 app.register_blueprint(bp) # same bp, same name, warning957 with pytest.raises(ValueError):958 app.register_blueprint(bp) # same bp, same name, error959960 app.register_blueprint(bp, name="again") # same bp, different name, ok961962 with pytest.raises(ValueError):
41 unmodified lines
modified:CHANGES.rst+2 added
15 unmodified lines
16 - ``safe_join`` is removed, use ``werkzeug.utils.safe_join``17 instead.18 - ``total_seconds`` is removed, use ``timedelta.total_seconds``19 instead.20 - The same blueprint cannot be registered with the same name. Use21 ``name=`` when registering to specify a unique name.222324Version 2.0.225-------------
1180 unmodified lines