Skip to content
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

Make Session.cookies a property, use session._cookies for Cookies object #446

Merged
merged 1 commit into from
Dec 1, 2024

Conversation

Velocidensity
Copy link
Contributor

This allows users to replace session.cookies after initialization with any CookieTypes object. This behavior matches httpx and requests, and allows it to be a drop-in replacement even in less typical usecases.

httpx uses property in the same manner:
https://github.com/encode/httpx/blob/47f4a96ffaaaa07dca1614409549b5d7a6e7af49/httpx/_client.py#L317-L326

requests appears to differ at first glance, as it does not use a property, but each request calls merge_cookies, which converts dicts to cookie jars, so it's virtually identical

https://github.com/psf/requests/blob/23540c93cac97c763fe59e843a08fa2825aa80fd/src/requests/sessions.py#L241
https://github.com/psf/requests/blob/23540c93cac97c763fe59e843a08fa2825aa80fd/src/requests/cookies.py#L552-L553

This allows users to replace session.cookies after initialization with any CookieTypes object.
This behavior matches httpx and requests, and allows it to be a drop-in replacement even in less typical usecases.

httpx uses property in the same manner:
https://github.com/encode/httpx/blob/47f4a96/httpx/_client.py#L317-L326

requests appears to differ at first glance, as it does not use a property,
but each request calls merge_cookies, which converts dicts to cookie jars, so it's virtually identical
https://github.com/psf/requests/blob/23540c9/src/requests/sessions.py#L241
https://github.com/psf/requests/blob/23540c9/src/requests/cookies.py#L552-L553
@lexiforest
Copy link
Owner

Do you have a example usage of the new behavior?

@Velocidensity
Copy link
Contributor Author

from curl_cffi import requests
from http.cookiejar import MozillaCookieJar

session = requests.Session()
cookies = MozillaCookieJar('blabla.txt')
cookies.load()

session.cookies = cookies

This is a basic example, usually Session gets initialised in a parent class, which makes overriding like this necessary.

@lexiforest lexiforest merged commit 4193027 into lexiforest:main Dec 1, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants