Skip to content

Commit

Permalink
Added 'partitioned' parameter to set_cookie method. (#10371)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpjacobpadilla authored Feb 3, 2025
1 parent 5bb6e95 commit f46ccff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/9870.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for the ``partitioned`` attribute in the ``set_cookie`` method.
4 changes: 4 additions & 0 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ def set_cookie(
secure: Optional[bool] = None,
httponly: Optional[bool] = None,
samesite: Optional[str] = None,
partitioned: Optional[bool] = None,
) -> None:
"""Set or update response cookie.
Expand Down Expand Up @@ -974,6 +975,9 @@ def set_cookie(
if samesite is not None:
c["samesite"] = samesite

if partitioned is not None:
c["partitioned"] = partitioned

if DEBUG:
cookie_length = len(c.output(header="")[1:])
if cookie_length > COOKIE_MAX_LENGTH:
Expand Down
8 changes: 7 additions & 1 deletion docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ and :ref:`aiohttp-web-signals` handlers::

.. method:: set_cookie(name, value, *, path='/', expires=None, \
domain=None, max_age=None, \
secure=None, httponly=None, samesite=None)
secure=None, httponly=None, samesite=None, \
partitioned=None)

Convenient way for setting :attr:`cookies`, allows to specify
some additional properties like *max_age* in a single call.
Expand Down Expand Up @@ -753,6 +754,11 @@ and :ref:`aiohttp-web-signals` handlers::

.. versionadded:: 3.7

:param bool partitioned: ``True`` to set a partitioned cookie.
Available in Python 3.14+. (optional)

.. versionadded:: 3.12

.. method:: del_cookie(name, *, path='/', domain=None)

Deletes cookie.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,19 @@ def test_cookies_mixin_path() -> None:
)


@pytest.mark.skipif(sys.version_info < (3, 14), reason="No partitioned support")
def test_cookies_mixin_partitioned() -> None:
sut = CookieImplementation()

assert sut.cookies == {}

sut.set_cookie("name", "value", partitioned=False)
assert str(sut.cookies) == "Set-Cookie: name=value; Path=/"

sut.set_cookie("name", "value", partitioned=True)
assert str(sut.cookies) == "Set-Cookie: name=value; Partitioned; Path=/"


def test_sutonse_cookie__issue_del_cookie() -> None:
sut = CookieImplementation()

Expand Down

0 comments on commit f46ccff

Please sign in to comment.