pallets/flask · error · TypeError
Cookies are disabled. Create a client with 'use_cookies=True
Error message
Cookies are disabled. Create a client with 'use_cookies=True'.
What it means
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.
Source
Thrown at src/flask/testing.py:156
) -> t.Iterator[SessionMixin]:
"""When used in combination with a ``with`` statement this opens a
session transaction. This can be used to modify the session that
the test client uses. Once the ``with`` block is left the session is
stored back.
::
with client.session_transaction() as session:
session['value'] = 42
Internally this is implemented by going through a temporary test
request context and since session handling could depend on
request variables this function accepts the same arguments as
:meth:`~flask.Flask.test_request_context` which are directly
passed through.
"""
if self._cookies is None:
raise TypeError(
"Cookies are disabled. Create a client with 'use_cookies=True'."
)
app = self.application
ctx = app.test_request_context(*args, **kwargs)
self._add_cookies_to_wsgi(ctx.request.environ)
with ctx:
sess = app.session_interface.open_session(app, ctx.request)
if sess is None:
raise RuntimeError("Session backend did not open a session.")
yield sess
resp = app.response_class()
if app.session_interface.is_null_session(sess):
returnView on GitHub ↗ (pinned to 6a2f545bfd)
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.
Example fix
# before
client = app.test_client(use_cookies=False)
with client.session_transaction() as sess:
sess["user_id"] = 1
# after
client = app.test_client()
with client.session_transaction() as sess:
sess["user_id"] = 1 Defensive patterns
Strategy: validation
Validate before calling
client = app.test_client(use_cookies=True) # only then use client.session_transaction()
Type guard
def cookies_enabled(client) -> bool:
return getattr(client, '_cookies', None) is not None Try / catch
try:
with client.session_transaction() as sess:
sess['user_id'] = 1
except TypeError:
# client was created with use_cookies=False
raise Prevention
- 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
When it happens
Trigger: Calling `with client.session_transaction() as sess:` on a client constructed via `app.test_client(use_cookies=False)` or `FlaskClient(app, use_cookies=False)`.
Common situations: 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.
Related errors
- Session backend did not open a session.
- The session is unavailable because no secret key was set. S
- Cannot pop this context ({self!r}), it is not pushed.
- Tag '{key}' is already registered.
- Cannot nest client invocations
AI-assisted analysis of pallets/flask@6a2f545bfd (2026-07-31).
Data as JSON: /data/errors/081d89dcc4e312ce.json.
Report an issue: GitHub ↗.