Skip to content

Added 'partitioned' parameter to set_cookie method. #10371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
if samesite is not None:
c["samesite"] = samesite

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

Check warning on line 979 in aiohttp/helpers.py

View check run for this annotation

Codecov / codecov/patch

aiohttp/helpers.py#L979

Added line #L979 was not covered by tests

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 @@
)


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

Check warning on line 981 in tests/test_helpers.py

View check run for this annotation

Codecov / codecov/patch

tests/test_helpers.py#L981

Added line #L981 was not covered by tests

assert sut.cookies == {}

Check warning on line 983 in tests/test_helpers.py

View check run for this annotation

Codecov / codecov/patch

tests/test_helpers.py#L983

Added line #L983 was not covered by tests

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

Check warning on line 986 in tests/test_helpers.py

View check run for this annotation

Codecov / codecov/patch

tests/test_helpers.py#L985-L986

Added lines #L985 - L986 were not covered by tests

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

Check warning on line 989 in tests/test_helpers.py

View check run for this annotation

Codecov / codecov/patch

tests/test_helpers.py#L988-L989

Added lines #L988 - L989 were not covered by tests


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

Expand Down
Loading