Skip to content

use properties to allow more easily changing cookies/headers after init - #274

Merged
sethmlarson merged 3 commits into
encode:masterfrom
toppk:master
Aug 28, 2019
Merged

sethmlarson merged 3 commits into
encode:masterfrom
toppk:master

Conversation

@toppk

@toppk toppk commented Aug 24, 2019

Copy link
Copy Markdown
Contributor

coming from requests, I was used to doing session.cookies = LWPCookieJar()
with httpx, I have to do:
client.cookies = httpx.models.Cookies(LWPCookieJar())
or I can just do this as part of the Client constructor.

This doesn't solve the second half of the problem, where now I have to do:
client.cookies.jar.save()
instead of
session.cookies.save()

But just thought I'd through this patch in there. I suspect there will be others looking to do something similar. This is easy enough to workaround.

@sethmlarson

Copy link
Copy Markdown
Contributor

Thanks for opening this! This is definitely a question of Requests compatibility which is something we want to maintain. I have no issue with this change but I've never used cookies/cookiejars with Requests so I don't know much in this area. cc @tomchristie

@florimondmanca florimondmanca added the help wanted Extra attention is needed label Aug 26, 2019
@lovelydinosaur

Copy link
Copy Markdown
Member

I'm not sure about this change.

It seems fairly reasonable to me to force the usage of:

client.cookies = httpx.Cookies(LWPCookieJar())

Since the type that the property ends up being is an httpx.Cookies instance. It might(?) be a bit weird for us to simply coerce property assignments.

client.cookies = LWPCookieJar()
isinstance(client.cookies, LWPCookieJar). # False

On the other hand, perhaps it's worth it for simplicity.

I think a good way of reviewing if we want this or not would be to ask the same question of other assignments. Eg. Do we want to support this...

client.headers = {...}

I guess the answer to that is "yeah, sure", in which case we probably want the same from .cookies. Thoughts, anyone?

@sethmlarson

Copy link
Copy Markdown
Contributor

It seems like a good idea to me, it's what we already do for a lot of __init__ arguments.

@lovelydinosaur

Copy link
Copy Markdown
Member

Okay, then I'm a "yes" to this pull request, but I'd like it to be updated to handle both .cookies and .headers in the same way, for consistency.

@florimondmanca

Copy link
Copy Markdown
Contributor

I'd be okay with this (coercing property assignments of cookies and headers) as well.

To address the "second half of the problem" mentioned by @toppk (which does exist if we go down this road, I think?), we should probably update the Client API to document that client.cookies and client.headers are always returned as a Cookies/Headers instance.

@sethmlarson

Copy link
Copy Markdown
Contributor

@toppk Are you comfortable making the changes requested above yourself or would you like that to be done in a separate PR?

@toppk

toppk commented Aug 27, 2019

Copy link
Copy Markdown
Contributor Author

i'll give it a shot. I think we can use the same PR.

@toppk

toppk commented Aug 27, 2019

Copy link
Copy Markdown
Contributor Author

added typing information, api documentation, and repeated for headers

rebased for a cleaner commit,

@toppk toppk changed the title use properties to allow more easily changing cookies after init use properties to allow more easily changing cookies/headers after init Aug 27, 2019

@lovelydinosaur lovelydinosaur 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.

Looking great. One minor set of changes and I think we鈥檙e there. Nice one!

Comment thread httpx/client.py Outdated
) -> typing.Optional[CookieTypes]:
if cookies or self.cookies:
merged_cookies = Cookies(self.cookies)
if cookies or self._cookies:

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.

On reflection I think we should just use the public self.cookies/self.headers accessors for these cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done. Do you want me to do that in the constructor as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably best to make sure the implementations for turning user-input into our wrapped classes are identical for constructor or setter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

my mypy foo is lacking. I get an error message if I update the construct to use the property.

httpx/client.py:105: error: Incompatible types in assignment (expression has type "Union[Headers, Dict[Any, Any], List[Tuple[Any, Any]], None]", variable has type "Headers")

i've left it as is for now.

Comment thread httpx/client.py Outdated
@sethmlarson

Copy link
Copy Markdown
Contributor

Also: Can we get two simple unit tests to see that objects are transformed properly :)

- use properties for Client.headers and Client.cookies
- update the Client API docs to document the headers and cookies
- add Client model tests for setting headers and cookies from dict
- add client cookie test that sets cookies

@sethmlarson sethmlarson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two small review comments then this PR looks good to me.

Comment thread tests/models/test_client.py
Comment thread tests/client/test_cookies.py Outdated

@sethmlarson sethmlarson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As long as CI passes I'll merge! Thanks! :)

@sethmlarson
sethmlarson merged commit 9e420a5 into encode:master Aug 28, 2019
@sethmlarson

Copy link
Copy Markdown
Contributor

Hope you'll stick around and try tackling some other issues @toppk! :)

@florimondmanca

Copy link
Copy Markdown
Contributor

Nice work @toppk and @sethmlarson on this one 馃憤

@lovelydinosaur lovelydinosaur mentioned this pull request Jan 6, 2020
8 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Extra attention is needed

Development

Successfully merging this pull request may close these issues.

4 participants