Skip to content

Type cleanup#567

Merged
wch merged 77 commits into
type-strictfrom
type-cleanup
Apr 4, 2024
Merged

Type cleanup#567
wch merged 77 commits into
type-strictfrom
type-cleanup

Conversation

@wch
Copy link
Copy Markdown
Collaborator

@wch wch commented Mar 28, 2024

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.

  • Before merging, change main.yml back so that workflow is run only on pull requests that target master.

Comment thread tests/test_main.py
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"},
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 3, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
4663 3508 75% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
rsconnect/actions.py 48% 🟢
rsconnect/actions_content.py 65% 🟢
rsconnect/api.py 71% 🟢
rsconnect/bundle.py 80% 🟢
rsconnect/environment.py 84% 🟢
rsconnect/http_support.py 83% 🟢
rsconnect/json_web_token.py 97% 🟢
rsconnect/log.py 69% 🟢
rsconnect/main.py 62% 🟢
rsconnect/metadata.py 91% 🟢
rsconnect/models.py 92% 🟢
rsconnect/utils_package.py 77% 🟢
TOTAL 77% 🟢

updated for commit: 89f855d by action🐍

@wch wch marked this pull request as ready for review April 3, 2024 23:36
@wch
Copy link
Copy Markdown
Collaborator Author

wch commented Apr 3, 2024

This is ready for a review and then we can merge it into the type-cleanup branch.

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 -, so that even if it fails, make will not interpret it as an error, and CI will keep running. Once the typing work is completed, this can be removed so that any pyright failures will lead to CI failures.

Comment thread rsconnect/api.py
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: ...
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.

Can you clarify the purpose of the overloads vs. just having the one method with a boolean parameter?

Copy link
Copy Markdown
Collaborator Author

@wch wch Apr 4, 2024

Choose a reason for hiding this comment

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

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 a HTTPResponse. (If the object was an HTTPResponse, the function throws.) In all cases where this is used, the input type is either a TypedDict or a HTTPResponse, and so when it returns, we can be certain that the result is the TypedDict.
  • When is_httpresponse=True, then the returned object could be an HTTPResponse. I added this because there are some instances where the input is expected to be an HTTPResponse. 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.

@wch wch merged commit f0b1cd9 into type-strict Apr 4, 2024
@wch wch deleted the type-cleanup branch April 4, 2024 16:03
@edavidaja edavidaja mentioned this pull request Apr 29, 2025
@karawoo karawoo mentioned this pull request May 29, 2026
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