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

[Mypy] Add more mypy #1564

Merged
merged 32 commits into from
Feb 24, 2023
Merged

[Mypy] Add more mypy #1564

merged 32 commits into from
Feb 24, 2023

Conversation

Michaelvll
Copy link
Collaborator

@Michaelvll Michaelvll commented Dec 30, 2022

This PR adds mypy checks for almost all the modules excluding the following files:

--exclude sky/benchmark
--exclude sky/callbacks
--exclude sky/skylet/providers # blocked by the ray upstream
--exclude sky/resources.py # blocked by #1429 
--exclude sky/global_user_state.py
--exclude sky/optimizer.py
--exclude sky/task.py

Except for the modules with comments above, we will add mypy checks for the rest of the files in a future PR.

Tested (run the relevant ones):

  • Any manual or new tests for this PR (please specify below)
    • mypy checks pass
  • All smoke tests: bash tests/run_smoke_tests.sh
  • Backward compatibility tests: bash tests/backward_comaptibility_tests.sh

@Michaelvll Michaelvll marked this pull request as ready for review December 30, 2022 19:53
Copy link
Collaborator

@romilbhardwaj romilbhardwaj left a comment

Choose a reason for hiding this comment

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

Thanks @Michaelvll! I've reviewed 16/36 files (up to execution.py). I can do more reviews late next week, but I would like someone else to look at this if they can get to it before 19th Jan.

format.sh Outdated
@@ -98,8 +98,6 @@ mypy $(cat tests/mypy_files.txt)
# Run Pylint
echo 'Sky Pylint:'
pylint --load-plugins pylint_quotes sky
Copy link
Collaborator

Choose a reason for hiding this comment

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

on running format.sh, I got an error:

(base) romilb@romilbx1yoga:/mnt/d/Romil/Berkeley/Research/sky-experiments$ ./format.sh             
SkyPilot mypy:
sky/authentication.py:187: error: No overload variant of "get" of "dict" matches argument types "str", "str"  [call-overload]
sky/authentication.py:187: note: Possible overload variants:
sky/authentication.py:187: note:     def get(self, <nothing>, /) -> None
sky/authentication.py:187: note:     def [_T] get(self, <nothing>, _T, /) -> _T
sky/authentication.py:249: error: No overload variant of "get" of "dict" matches argument types "str", "str"  [call-overload]
sky/authentication.py:249: note: Possible overload variants:
sky/authentication.py:249: note:     def get(self, <nothing>, /) -> None
sky/authentication.py:249: note:     def [_T] get(self, <nothing>, _T, /) -> _T
Found 2 errors in 1 file (checked 83 source files)

Here's my pip freeze if it helps

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, that is weird. Could you share the python version you are using?

sky/backends/backend_utils.py Show resolved Hide resolved
sky/backends/backend_utils.py Show resolved Hide resolved
@@ -2102,7 +2101,7 @@ def _provision(self,
raise exceptions.ResourcesUnavailableError(
error_message) from None
if dryrun:
return
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we change other instances of return to return None to be consistent? (as suggested in pep8 - "Be consistent in return statements"). E.g. at L2277

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added the return None for the LocalDockerBackend._provision as well, but for the return statement at L2277, the method has a return type annotation as -> None, so it seems to be fine (or more intuitive) to not having the return value in the return statement.

Seems the standard for being consistent for the return statement has a scope of each function, IIUC. It might be fine to only have the return statement consistent in a single function.

Either all return statements in a function should return an expression, or none of them should.

sky/cli.py Show resolved Hide resolved
sky/core.py Outdated Show resolved Hide resolved
@Michaelvll
Copy link
Collaborator Author

With the latest changes, I avoid the nested class of the ResourceHandle as we are using the type of the handle outside the outer class. There is no reason to keep it nested in the Backend.

@Michaelvll
Copy link
Collaborator Author

A kind reminder for this PR @romilbhardwaj. : )

Copy link
Collaborator

@romilbhardwaj romilbhardwaj left a comment

Choose a reason for hiding this comment

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

Thanks @Michaelvll! Sorry about the delay in getting to this. Left some comments, particularly need some more on clarity on why pyi stub files are needed. Rest looks great!

sky/setup_files/setup.py Show resolved Hide resolved
sky/skylet/log_lib.py Outdated Show resolved Hide resolved
sky/skylet/log_lib.py Show resolved Hide resolved
@@ -0,0 +1,108 @@
from sky import sky_logging as sky_logging
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a little new to mypy, but is this stub file necessary if we fully type log_lib.py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This stub file is mainly for the type hint of run_with_log where we want to determine the return type by the value of the argument require_outputs. To remove this stub file, we can move the two typing.overload back into the original log_lib.py, but that may make that file much longer. Wdyt?

Copy link
Collaborator

@romilbhardwaj romilbhardwaj Feb 21, 2023

Choose a reason for hiding this comment

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

Ahh... I think it would be nicer to move the overload definitions to log_lib.py (fewer files to deal with, easier to maintain).

On a related note - should we reconsider why we have different return types for run_with_log? It would be nice if we can get away with Tuple[int, str, str] and return proc.returncode, '', '' when require_outputs is false? Feel free to defer for later too

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

@Michaelvll Michaelvll Feb 22, 2023

Choose a reason for hiding this comment

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

Oops, sorry. Just realized, another reason we have to use the stub file is that the pylint has a bug with the type overload in the same file (reference). It will not correctly recognize the overloaded types and complain about sky/backends/cloud_vm_ray_backend.py:1362:8: E0633: Attempting to unpack a non-sequence (unpacking-non-sequence).
I tried to upgrade the pylint to the latest version but the problem still exists. I had to keep the stub file for now.

I am planning to refactor the require_outputs and remove the stub file in another PR as otherwise, the changes will be mixed with the mypy changes in this PR, making it hard to distinguish the changes. : )

sky/spot/spot_utils.py Show resolved Hide resolved
sky/utils/command_runner.pyi Show resolved Hide resolved
Copy link
Collaborator

@romilbhardwaj romilbhardwaj left a comment

Choose a reason for hiding this comment

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

Thanks for adding this @Michaelvll! This will help us catch bugs a lot faster : ) Should be good to merge if all tests pass.

SKY_RESERVED_CLUSTER_NAMES = {
spot_lib.SPOT_CONTROLLER_NAME: 'Managed spot controller'
SKY_RESERVED_CLUSTER_NAMES: Dict[str, str] = {
spot_lib.SPOT_CONTROLLER_NAME: 'Managed spot controller' # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

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

This # type: ignore seems not required - mypy passes without this too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, good catch! Removed it. Thanks!

@Michaelvll
Copy link
Collaborator Author

Michaelvll commented Feb 24, 2023

Thanks for the review @romilbhardwaj! Tested:

  • pytest tests/test_smoke.py
  • sky commands in a new container.

@Michaelvll Michaelvll merged commit 22397b1 into master Feb 24, 2023
@Michaelvll Michaelvll deleted the more-mypy branch February 24, 2023 20:06
sumanthgenz pushed a commit to sumanthgenz/skypilot that referenced this pull request Mar 15, 2023
* backend_utils mypy

* More mypy

* format

* order

* change to exclude

* remove unused registry.py

* add back task.py

* dependency

* quote

* fix circular import

* fix

* remove unused

* fix issue with master

* refactor ResourceHandle

* fix

* dryrun for local docker backend

* lint

* fix merge issue

* Address comments

* lint

* format

* remote type ignore

* add typing extensions to the dependency
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