Skip to content

feat: add cookies and host_only_cookies properties to CookieJar#12174

Merged
bdraco merged 8 commits into
aio-libs:masterfrom
Br1an67:feat/expose-cookiejar-cookies
May 20, 2026
Merged

feat: add cookies and host_only_cookies properties to CookieJar#12174
bdraco merged 8 commits into
aio-libs:masterfrom
Br1an67:feat/expose-cookiejar-cookies

Conversation

@Br1an67

@Br1an67 Br1an67 commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

What do these changes do?

Add read-only cookies and host_only_cookies properties to CookieJar (and AbstractCookieJar / DummyCookieJar) so users can access stored cookies with their full attributes (expires, domain, path, etc.) without relying on private _cookies / _host_only_cookies.

  • cookies returns MappingProxyType[tuple[str, str], SimpleCookie] — an immutable view of the internal cookie storage
  • host_only_cookies returns frozenset[tuple[str, str]] — a frozen copy of the host-only cookie set

This addresses the long-standing request to expose cookie metadata that filter_cookies() intentionally strips (since the HTTP Cookie header does not include attributes like expires).

Are there changes in behavior for the user?

Two new public read-only properties are available on CookieJar and DummyCookieJar:

  • jar.cookies — read-only mapping of (domain, path) -> SimpleCookie
  • jar.host_only_cookies — frozen set of (domain, name) tuples

No existing behavior is changed.

Is it a substantial burden for the maintainers to support this?

Minimal. The properties are thin wrappers (MappingProxyType / frozenset) over existing internal state with no new data structures. The MappingProxyType prevents direct mutation of the mapping, though the underlying SimpleCookie values are still mutable — this matches the trade-off discussed in the issue.

Related issue number

Fixes #3951

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

Add read-only properties to expose stored cookies with their full
attributes (expires, domain, path, etc.). The cookies property returns
a MappingProxyType for immutability, and host_only_cookies returns a
frozenset.

Fixes aio-libs#3951
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Mar 1, 2026
@codspeed-hq

codspeed-hq Bot commented Mar 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 72 untouched benchmarks
⏩ 72 skipped benchmarks1


Comparing Br1an67:feat/expose-cookiejar-cookies (5fc4d47) with master (f69522c)

Open in CodSpeed

Footnotes

  1. 72 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov

codecov Bot commented Mar 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.95%. Comparing base (f69522c) to head (5fc4d47).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12174   +/-   ##
=======================================
  Coverage   98.95%   98.95%           
=======================================
  Files         131      131           
  Lines       46627    46688   +61     
  Branches     2418     2421    +3     
=======================================
+ Hits        46139    46200   +61     
  Misses        366      366           
  Partials      122      122           
Flag Coverage Δ
Autobahn 22.42% <36.50%> (+0.01%) ⬆️
CI-GHA 98.92% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.67% <100.00%> (-0.01%) ⬇️
OS-Windows 97.03% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.93% <100.00%> (+<0.01%) ⬆️
Py-3.10 98.15% <100.00%> (+<0.01%) ⬆️
Py-3.11 98.42% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.50% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.47% <100.00%> (+<0.01%) ⬆️
Py-3.14 98.49% <100.00%> (-0.01%) ⬇️
Py-3.14t 97.55% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.11 97.42% <100.00%> (-0.01%) ⬇️
VM-macos 97.93% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.67% <100.00%> (-0.01%) ⬇️
VM-windows 97.03% <100.00%> (+<0.01%) ⬆️
cython-coverage 37.94% <26.98%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Dreamsorcerer Dreamsorcerer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on @bdraco's proposal, this looks good to me. I'll let him give it final sign-off.

@Dreamsorcerer Dreamsorcerer requested a review from bdraco March 1, 2026 12:34
@Dreamsorcerer Dreamsorcerer added the backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot label Mar 1, 2026
Comment thread aiohttp/cookiejar.py
Comment thread docs/client_reference.rst Outdated
Comment thread docs/client_reference.rst Outdated
bdraco and others added 2 commits May 19, 2026 19:35
Co-authored-by: J. Nick Koston <nick+github@koston.org>
…ejar-cookies

# Conflicts:
#	tests/test_client_session.py
@bdraco

bdraco commented May 20, 2026

Copy link
Copy Markdown
Member

Thanks @Br1an67

@bdraco bdraco merged commit ca9d73d into aio-libs:master May 20, 2026
45 checks passed
@patchback

patchback Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.14: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply ca9d73d on top of patchback/backports/3.14/ca9d73ddf1e2a182e058641561b7766186b30485/pr-12174

Backporting merged PR #12174 into master

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/aio-libs/aiohttp.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/3.14/ca9d73ddf1e2a182e058641561b7766186b30485/pr-12174 upstream/3.14
  4. Now, cherry-pick PR feat: add cookies and host_only_cookies properties to CookieJar #12174 contents into that branch:
    $ git cherry-pick -x ca9d73ddf1e2a182e058641561b7766186b30485
    If it'll yell at you with something like fatal: Commit ca9d73ddf1e2a182e058641561b7766186b30485 is a merge but no -m option was given., add -m 1 as follows instead:
    $ git cherry-pick -m1 -x ca9d73ddf1e2a182e058641561b7766186b30485
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR feat: add cookies and host_only_cookies properties to CookieJar #12174 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/3.14/ca9d73ddf1e2a182e058641561b7766186b30485/pr-12174
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

bdraco added a commit that referenced this pull request May 20, 2026
…ookies properties to CookieJar (#12662)

Co-authored-by: Br1an <932039080@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Fixes #3951
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CookieJar filter_cookies does not preserve expiration date

3 participants