pallets/flask · error · TypeError
Cookies are disabled. Create a client with…
Error message
Cookies are disabled. Create a client with 'use_cookies=True'.
What it means
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.
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.
Example fix
// before
client = app.test_client(use_cookies=False)
with client.session_transaction() as sess:
sess['k'] = 1 # raises
// after
client = app.test_client(use_cookies=True)
with client.session_transaction() as sess:
sess['k'] = 1 Defensive patterns
Strategy: validation
Validate before calling
def require_cookies_enabled(client):
if getattr(client, '_cookies', None) is None:
raise TypeError('client has cookies disabled; pass use_cookies=True')
Type guard
def client_has_cookies(client) -> bool:
return getattr(client, '_cookies', None) is not None
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Session backend did not open a session.
- Tag ' ' is already registered.
- The session is unavailable because no secret key was set. …
- A valid Flask application was not obtained from
- 'after_this_request' can only be used when a request…
AI-assisted analysis of pallets/flask@3596b1ab61 (2026-08-11).
Data as JSON: /api/errors/081d89dcc4e312ce.
Report an issue: GitHub.
Appendix: 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 3596b1ab61)