-
-
Notifications
You must be signed in to change notification settings - Fork 932
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
Replace HTTP client on TestClient from requests
to httpx
#1376
Conversation
4cb3f1a
to
9348fb2
Compare
9348fb2
to
067e8e6
Compare
How do I send the headers in See https://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file |
Test failing until encode/httpx#1936 is merged. |
Great bit of work, thanks! We'll want some careful decision making before we hit go on this. Particularly around what FastAPI users will expect. Something that we could choose to do here would be switch from |
About the deprecation, anything else besides the item specified in the description of this PR and the As a reminder, this PR cannot be concluded without taking a decision on encode/httpx#1936, or giving me orientation e.g. I can skip the test that involves that issue. |
Indeed, so for now let's hold on that.
Well essentially it'd be interesting to consider two options...
A useful thing to consider here is that any changes to the footprint of our test suite here is a good indicator of what changes our users would also need to make to their test suites. If we're hitting them with a big set of required changes in order to upgrade, then that's not fantastic. (Although there might or might not be good enough reasons why we're prefer to do that.) I'm mostly saying all this in order to flag up that I think we'll want @tiangolo's perspective, and that we will need to carefully look at the cost of API changes, and how we can mitigate that. |
setup.py
Outdated
# "httpx>=0.20.0" | ||
"httpx @ git+https://github.com/encode/httpx.git@master", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until next httpx release. This is a blocker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could httpx cut a release? This is going to cause issues for anyone that depends on httpx right? I realize you can probably just not install the "full" extra, but this still breaks things for those that are already doing it / it's a bit unergonomic and confusing.
We usually follow a "try to not add breaking changes" approach, even if we are on "zero version". Considering this, we should make it backwards compatible. That being said... I'm going to list the set of breaking changes, so it can help us to make the best decision here:
|
@tomchristie friendly follow-up ping |
Awesome! 🚀 Thanks a lot @Kludex for the PR and for the short summary of breaking changes, that's super helpful. Indeed, breaking changes are not great in general... 😅 . Nevertheless, the intention has been to move from Requests to HTTPX from the beginning, I would like to do as much as possible to support this. Some comments about the changes:
|
Thanks for your input @tiangolo ! :) I'll wait for @tomchristie 's feedback, so we can decide how to continue here in order to have this PR merged. |
aa1e3a3
to
38d8204
Compare
I asked because the Starlette starlette/starlette/testclient.py Line 711 in 3050e9e
I don't have a specific problem with it in this context, just wanted to raise the question for discussion. |
So... the largest impact of this change will be for FastAPI devs. They're the largest install base, they're using the test client, and the change will introduce breaking changes into their test suites and require extra work for their teams. I guess we might(?) want to have a couple of shims in place in order to minimise the API changes...
We could also consider assessing how much negative impact this PR would have by making a pull request to the FastAPI project that uses this branch for the TestClient, and determining how much work is required to change the test suite there. Anyways, I don't know really - not super-keen on giving teams extra busywork, but yes it would be nicer to have switched to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! I'm all in for this. 🚀
I just tested it locally in FastAPI, here's the PR with the changes needed after this goes in (and I upgrade the Starlette version pinned in FastAPI): fastapi/fastapi#5350
There are indeed some changes, but I think they are all sensible, and codebases that need to upgrade would have cleaner code after that.
I would think that the main changes I would expect would be:
- Changing
data
tocontent
where needed - Changing the style of form data from a list of tuples to a dict, which I think is much more cleaner and understandable
- Changing inline cookies with cookies at client creation
I wouldn't expect many to need to change follow_redirects
because I don't think many would even use it.
I also don't expect many to need to send body payloads with GET, DELETE or others, just in corner cases, and they can achieve it as Tom described above (and as done in that FastAPI PR).
Also, for almost all of the points, I think except for the cookies in the client, the needed refactor could be automated with a LibCST codemod... but who would have the expertise to end up building that?! 😱 😉
I have a couple of minor comments. But this looks great to me. Awesome job @Kludex! 🙇 🍰 ☕
Also, thank you @tomchristie for having me in mind! 🙇 ☕
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Nice job @Kludex
This seems like a useful route to take and is quite common in the JavaScript ecosystem. |
Thanks!
If no one does it before me, I'll do it before the next release. 👍 |
I'd be interested in a blog post on AST transforms 😉 |
Hehe, I was actually joking with the "who would have the expertise", I knew @Kludex was the - right - person. 😎 I was just putting the "inception" idea seed. 😅 |
From the changes:
I've implemented 1, 2, and 5. I need to add more tests, and fix a scenario, but in progress.
Code in https://github.com/Kludex/bump-testclient. EDIT: I thought of a way to do 3. 🤔 |
Are there any technical reasons why |
fix/upgrade dependencies upgrades python version to 3.10.5 upgrades dependencies to their latest versions this is needed because of pinned httpx version is outdated and TestClient breaks starlette and FastAPI references encode/starlette#1376 fastapi/fastapi#5471 fastapi/fastapi#5749
cf. encode/starlette#1376, use `follow_redirects`
seemingly, this test hits a fallback path for resolving access urls and that fallback requires an API client; this issue may have started when starlette (ASGI) framework switched from requests to httpx. > So... the largest impact of this change will be for FastAPI devs. They're the largest install base, they're using the test client, and the change will introduce breaking changes into their test suites and require extra work for their teams. -- encode/starlette#1376 (comment) possible mitigations: upgrade fastapi, as per notes in notes in fastapi/fastapi#5350
Missing:
asgiref
.import typing
requests
.