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

Add comprehensive type annotations #566

Merged
merged 86 commits into from
Apr 5, 2024
Merged

Add comprehensive type annotations #566

merged 86 commits into from
Apr 5, 2024

Conversation

wch
Copy link
Collaborator

@wch wch commented Mar 28, 2024

This PR follows up on a discussion I had with @mmarchetti. It does the following:

  • Turns on strict type checking with pyright.
  • Adds many type annotations. These by and large should not affect run-time behavior, and so should be safe.

In the changes here, there are still many, many issues flagged by pyright in strict mode.
The purpose of this is to set things up for a second pull request, which will be made against this pull request instead of master. The second PR will have changes that are not quite as safe as this PR -- it will add more type annotations and makes some changes to the logic, and is therefore not as safe as this PR. The reason they're done with two separate pull requests is so that the substantive changes in the second PR are more visible and not drowned out by the changes in this PR, which are safe. This will make the second PR easier to review.

After the second PR is merged into this one and tests are passing, we can merge this PR into master.

The overall goal is to get the code passing with pyright in strict mode in CI, and disable mypy. Once this is all done, it will be much easier to contribute to this repository and refactor the code here.

Copy link

github-actions bot commented Apr 4, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
4699 3512 75% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
rsconnect/actions.py 41% 🟢
rsconnect/actions_content.py 65% 🟢
rsconnect/api.py 71% 🟢
rsconnect/bundle.py 80% 🟢
rsconnect/environment.py 84% 🟢
rsconnect/exception.py 100% 🟢
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/shiny_express.py 75% 🟢
rsconnect/timeouts.py 100% 🟢
rsconnect/utils_package.py 77% 🟢
rsconnect/validation.py 86% 🟢
TOTAL 80% 🟢

updated for commit: 662ae1d by action🐍

@wch wch marked this pull request as ready for review April 4, 2024 16:24
return deploy_app(app_mode=AppModes.PYTHON_API, **locals())


def deploy_python_fastapi(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function is imported by vetiver-python and its removal may be why the tests are failing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, that's good to know! This makes me a little concerned, though: I removed many of the functions in actions.py because they were not used internally and seemed to essentially be duplicates of functions in main.py. Do we need to keep any other of those functions around in case someone external was using them?

Also, in this particular case, I wonder if we can just have actions.deploy_python_fastapi be an alias to main.deploy_python_fastapi.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we can just have actions.deploy_python_fastapi be an alias to main.deploy_python_fastapi

Yes, as long as we're careful to match the type signature that Vetiver is expecting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm, there currently isn't a main.deploy_python_fastapi; the command line command is generated by the following and isn't saved in a variable anywhere:

generate_deploy_python(app_mode=AppModes.PYTHON_FASTAPI, alias="fastapi", min_version="2021.08.0")

Also, the function signature is somewhat different and I don't want to risk breaking things, so for now I'll just bring back this code.

@wch wch mentioned this pull request Apr 4, 2024
@wch
Copy link
Collaborator Author

wch commented Apr 4, 2024

From some testing I did in #571, it looks like commit c476d69 made the test break. It looks like something on the server side stopped working?

In that commit I added many type annotations, but didn't make any functional changes. I suppose it's possible that some of the syntax I used there was not compatible with Python 3.8.

This is the line that cause the error:

python vetiver-testing/setup-rsconnect/dump_api_keys.py vetiver-testing/rsconnect_api_keys.json

As far as I can tell, that code does not cause rsconnect to be imported at all, at least not on the client side. But the server side gives errors in response to requests. Does rsconnect get loaded on the server side somehow?

@wch
Copy link
Collaborator Author

wch commented Apr 5, 2024

Now when I run the tests, the results are inconsistent. For example, on the same commit, I ran the workflow twice, and the failure happened differently between the two runs.

I don't understand why the first one failed, but the second run failed in a way that makes sense to me and that I can fix.

@wch
Copy link
Collaborator Author

wch commented Apr 5, 2024

I fixed the bug mentioned in the previous comment, and now here's a case where it fails on the first run and succeeds on the second:

  1. https://github.com/rstudio/rsconnect-python/actions/runs/8562909967/job/23467049557
  2. https://github.com/rstudio/rsconnect-python/actions/runs/8562909967/job/23467122442

I see that in #569, where the command was switched from docker-compose to docker compose, it had the same failure on the first attempt and then passed on the second attempt, so that makes me more confident that the failures aren't related to the content of this PR.

  1. https://github.com/rstudio/rsconnect-python/actions/runs/8528435857/job/23362057518
  2. https://github.com/rstudio/rsconnect-python/actions/runs/8528435857/job/23365251904

I suspect there's a timing problem with line docker compose up -d and the commands that come after it -- it might not have enough time to reliably start the container before the subsequent requests are made to the container. In 916535f I added a sleep 4 after that line, and now it seems to be running more reliably -- two successes in a row.

@aronatkins
Copy link
Collaborator

The timing problem is likely #448. CC @tdstein

@wch wch merged commit 550b373 into master Apr 5, 2024
15 checks passed
@wch wch deleted the type-strict branch April 5, 2024 16:15
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.

3 participants