Type cleanup#567
Conversation
| httpretty.GET, | ||
| "https://api.shinyapps.io/v1/users/me", | ||
| body=open("tests/testdata/rstudio-responses/get-user.json", "r").read(), | ||
| adding_headers={"Content-Type": "application/json"}, |
There was a problem hiding this comment.
For tests to pass, I added this "Content-Type": "application/json" header to some of the mock HTTP endpoints. I believe that's the correct behavior to mock the endpoint.
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
|
This is ready for a review and then we can merge it into the There are still some typing issues that remain to be fixed, but the vast majority of the code now is fully typed. I think it's better to merge this sooner than later, because the typing updates will benefit other developers, and if we leave this branch open for a long time, there will be merge conflicts. After this is merged, then we can gradually fix the remaining issues. Note that the pyright test in the Makefile is currently run with a leading |
| def handle_bad_response(self, response: HTTPResponse, is_httpresponse: Literal[True]) -> HTTPResponse: ... | ||
|
|
||
| @overload | ||
| def handle_bad_response(self, response: HTTPResponse | T, is_httpresponse: Literal[False] = False) -> T: ... |
There was a problem hiding this comment.
Can you clarify the purpose of the overloads vs. just having the one method with a boolean parameter?
There was a problem hiding this comment.
The purpose of the overloads is for type narrowing. Keep in mind that this function takes a response object and either throws or returns that same object.
- When
is_httpresponse=False(the default), then the type checker can be certain that, the returned object is not aHTTPResponse. (If the object was anHTTPResponse, the function throws.) In all cases where this is used, the input type is either aTypedDictor aHTTPResponse, and so when it returns, we can be certain that the result is theTypedDict. - When
is_httpresponse=True, then the returned object could be anHTTPResponse. I added this because there are some instances where the input is expected to be anHTTPResponse. These are cases where the reponse status is 2xx, but the payload is not JSON; instead, it may be the contents of a file.
This stuff is a consequence of the _do_request method, which may return either an HTTPResponse or, if the reponse body is JSON, it will convert that to a dict and return that. The mix of possible return values makes some things downstream of that a bit awkward. I think that code would benefit from being restructured in a future PR.
This is the second pull request described in #566. It adds many types and changes to the logic. The goal is to pass pyright strict mode type checks.
master.