{"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/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/testing.py#L138-L174","documentation":"Raised as a TypeError by `FlaskClient.session_transaction()` when the test client was created with cookies disabled (`self._cookies is None`). Session transactions work by reading the session cookie, letting you mutate the session, then writing the resulting Set-Cookie back into the client's cookie jar — none of which is possible without cookie support.","triggerScenarios":"Calling `with client.session_transaction() as sess:` on a client constructed via `app.test_client(use_cookies=False)` or `FlaskClient(app, use_cookies=False)`.","commonSituations":"Test suites that disable cookies globally for stateless API testing, then later add a test needing session manipulation; shared pytest fixtures where `use_cookies=False` was set for one test class and reused elsewhere.","solutions":["Create the client with cookies enabled (the default): `client = app.test_client()` or explicitly `app.test_client(use_cookies=True)`.","Use a separate cookie-enabled client fixture for tests that touch the session, keeping the cookie-less one for pure API tests."],"exampleFix":"# before\nclient = app.test_client(use_cookies=False)\nwith client.session_transaction() as sess:\n    sess[\"user_id\"] = 1\n# after\nclient = app.test_client()\nwith client.session_transaction() as sess:\n    sess[\"user_id\"] = 1","handlingStrategy":"validation","validationCode":"client = app.test_client(use_cookies=True)\n# only then use client.session_transaction()","typeGuard":"def cookies_enabled(client) -> bool:\n    return getattr(client, '_cookies', None) is not None","tryCatchPattern":"try:\n    with client.session_transaction() as sess:\n        sess['user_id'] = 1\nexcept TypeError:\n    # client was created with use_cookies=False\n    raise","preventionTips":["Default test clients have cookies enabled — don't pass use_cookies=False if the test touches sessions","Create session-dependent test fixtures with an explicit use_cookies=True client","Separate cookie-less API test clients from session test clients in fixtures"],"tags":["flask","testing","session","cookies"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}