{"record":{"id":"081d89dcc4e312ce","repo":"pallets/flask","slug":"cookies-are-disabled-create-a-client-with-use-co","errorCode":null,"errorMessage":"Cookies are disabled. Create a client with 'use_cookies=True'.","messagePattern":"Cookies are disabled\\. Create a client with 'use_cookies=True'\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/flask/testing.py","lineNumber":156,"sourceCode":"    ) -> t.Iterator[SessionMixin]:\n        \"\"\"When used in combination with a ``with`` statement this opens a\n        session transaction.  This can be used to modify the session that\n        the test client uses.  Once the ``with`` block is left the session is\n        stored back.\n\n        ::\n\n            with client.session_transaction() as session:\n                session['value'] = 42\n\n        Internally this is implemented by going through a temporary test\n        request context and since session handling could depend on\n        request variables this function accepts the same arguments as\n        :meth:`~flask.Flask.test_request_context` which are directly\n        passed through.\n        \"\"\"\n        if self._cookies is None:\n            raise TypeError(\n                \"Cookies are disabled. Create a client with 'use_cookies=True'.\"\n            )\n\n        app = self.application\n        ctx = app.test_request_context(*args, **kwargs)\n        self._add_cookies_to_wsgi(ctx.request.environ)\n\n        with ctx:\n            sess = app.session_interface.open_session(app, ctx.request)\n\n        if sess is None:\n            raise RuntimeError(\"Session backend did not open a session.\")\n\n        yield sess\n        resp = app.response_class()\n\n        if app.session_interface.is_null_session(sess):\n            return","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/pallets/flask/blob/3596b1ab61cea85edb8970e83ff61daa073facf8/src/flask/testing.py#L138-L174","documentation":"FlaskClient.session_transaction needs cookie support to bind the session to the request/response cycle. If the client was created with use_cookies=False (or cookies were explicitly disabled), self._cookies is None and the method raises TypeError immediately, before touching the session.","triggerScenarios":"Calling client.session_transaction() on a client built with use_cookies=False; calling it on app.test_client(use_cookies=False); a custom test client subclass that nulls _cookies.","commonSituations":"Newer Flask (>=3.1) made use_cookies explicit and some test helpers default differently; tests written against an older Flask that always had cookies; API-only test clients that opted out of cookies but then try session_transaction.","solutions":["Create the client with use_cookies=True: with app.test_client(use_cookies=True) as c: ...","Move session assertions to a route that reads/writes the session and assert on the response/cookie instead.","If using a custom session backend, drive it through real requests rather than session_transaction."],"exampleFix":"// before\nclient = app.test_client(use_cookies=False)\nwith client.session_transaction() as sess:\n    sess['k'] = 1  # raises\n\n// after\nclient = app.test_client(use_cookies=True)\nwith client.session_transaction() as sess:\n    sess['k'] = 1","handlingStrategy":"validation","validationCode":"def require_cookies_enabled(client):\n    if getattr(client, '_cookies', None) is None:\n        raise TypeError('client has cookies disabled; pass use_cookies=True')\n","typeGuard":"def client_has_cookies(client) -> bool:\n    return getattr(client, '_cookies', None) is not None\n","tryCatchPattern":null,"preventionTips":["Build test clients with use_cookies=True whenever sessions are involved.","Centralize client creation in a fixture with the correct flags.","Document use_cookies requirement for session_transaction in test helpers."],"tags":["flask","testing","session","client","cookies"],"backgroundTag":null,"analyzedSha":"3596b1ab61cea85edb8970e83ff61daa073facf8","analyzedAt":"2026-08-11T20:11:52.622Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}