{"id":"5ce18a7d043f8c7a","repo":"pallets/flask","slug":"cannot-register-a-blueprint-on-itself","errorCode":null,"errorMessage":"Cannot register a blueprint on itself","messagePattern":"Cannot register a blueprint on itself","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/flask/sansio/blueprints.py","lineNumber":270,"sourceCode":"        \"\"\"\n        return BlueprintSetupState(self, app, options, first_registration)\n\n    @setupmethod\n    def register_blueprint(self, blueprint: Blueprint, **options: t.Any) -> None:\n        \"\"\"Register a :class:`~flask.Blueprint` on this blueprint. Keyword\n        arguments passed to this method will override the defaults set\n        on the blueprint.\n\n        .. versionchanged:: 2.0.1\n            The ``name`` option can be used to change the (pre-dotted)\n            name the blueprint is registered with. This allows the same\n            blueprint to be registered multiple times with unique names\n            for ``url_for``.\n\n        .. versionadded:: 2.0\n        \"\"\"\n        if blueprint is self:\n            raise ValueError(\"Cannot register a blueprint on itself\")\n        self._blueprints.append((blueprint, options))\n\n    def register(self, app: App, options: dict[str, t.Any]) -> None:\n        \"\"\"Called by :meth:`Flask.register_blueprint` to register all\n        views and callbacks registered on the blueprint with the\n        application. Creates a :class:`.BlueprintSetupState` and calls\n        each :meth:`record` callback with it.\n\n        :param app: The application this blueprint is being registered\n            with.\n        :param options: Keyword arguments forwarded from\n            :meth:`~Flask.register_blueprint`.\n\n        .. versionchanged:: 2.3\n            Nested blueprints now correctly apply subdomains.\n\n        .. versionchanged:: 2.1\n            Registering the same blueprint with the same name multiple","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/sansio/blueprints.py#L252-L288","documentation":"A ValueError from `Blueprint.register_blueprint` (src/flask/sansio/blueprints.py:269-270), guarding the nested-blueprints feature (Flask 2.0). Registering a blueprint as a child of itself (`bp is self`) would create an infinite parent/child cycle when `register()` later walks `_blueprints` to mount children, so Flask rejects the identity case immediately. Note it only catches direct self-registration — the check is `blueprint is self`.","triggerScenarios":"`bp.register_blueprint(bp)` — passing the exact same Blueprint object to its own `register_blueprint`. Usually a typo where the parent was meant: `child.register_blueprint(child)` instead of `parent.register_blueprint(child)`.","commonSituations":"Copy-paste errors while wiring nested blueprint hierarchies; loops that programmatically mount a list of blueprints onto a parent where the parent itself is accidentally in the list; confusing `app.register_blueprint(bp)` with `bp.register_blueprint(...)`.","solutions":["Fix the receiver/argument mix-up: call `parent.register_blueprint(child)` with two distinct Blueprint objects, or `app.register_blueprint(bp)` to mount on the application.","If iterating a collection of blueprints to nest under a parent, exclude the parent from the collection."],"exampleFix":"# before\napi = Blueprint(\"api\", __name__)\napi.register_blueprint(api)\n\n# after\napi = Blueprint(\"api\", __name__)\nv1 = Blueprint(\"v1\", __name__)\napi.register_blueprint(v1, url_prefix=\"/v1\")","handlingStrategy":"validation","validationCode":"if child is parent:\n    raise ValueError('cannot nest blueprint on itself')\nparent.register_blueprint(child)","typeGuard":"def can_nest(parent, child):\n    return parent is not child","tryCatchPattern":null,"preventionTips":["When nesting blueprints dynamically (loops, plugin systems), check identity (`child is not parent`) before registering","Keep blueprint construction and nesting in one place so cycles are visible at review time"],"tags":["flask","python","blueprints","nesting","validation"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}