-
Notifications
You must be signed in to change notification settings - Fork 657
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
CI: add mypy to strict type check #3165
Open
beauxq
wants to merge
7
commits into
ArchipelagoMW:main
Choose a base branch
from
beauxq:add-mypy-to-ci
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
126b322
CI: add mypy to strict type check
beauxq ae0ae60
ooops
beauxq 2c159e5
Merge branch 'main' into add-mypy-to-ci
beauxq 0e4b08e
Apply suggestions from code review
beauxq fd6da38
Merge branch 'main' into add-mypy-to-ci
beauxq 6c065be
bump versions
beauxq 9a16d45
Merge branch 'main' into add-mypy-to-ci
beauxq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,60 @@ | ||
import json | ||
from pathlib import Path | ||
import subprocess | ||
from typing import Any, Dict, List, Union | ||
|
||
config = Path(__file__).parent / "pyright-config.json" | ||
source_dir = Path(__file__).parent | ||
config = source_dir / "pyright-config.json" | ||
|
||
command = ("pyright", "-p", str(config)) | ||
print(" ".join(command)) | ||
|
||
try: | ||
result = subprocess.run(command) | ||
except FileNotFoundError as e: | ||
print(f"{e} - Is pyright installed?") | ||
exit(1) | ||
def run_pyright() -> int: | ||
""" returns process exit code """ | ||
command = ("pyright", "-p", str(config)) | ||
print(" ".join(command)) | ||
|
||
exit(result.returncode) | ||
try: | ||
pyright_result = subprocess.run(command) | ||
except FileNotFoundError as e: | ||
print(f"{e} - Is pyright installed?") | ||
exit(1) | ||
return pyright_result.returncode | ||
|
||
|
||
def run_mypy() -> int: | ||
""" returns process exit code """ | ||
with open(config) as config_file: | ||
config_data: Union[Dict[str, Any], Any] = json.load(config_file) | ||
|
||
assert isinstance(config_data, dict) | ||
file_list: Union[List[str], None, Any] = config_data.get("include") | ||
assert isinstance(file_list, list), f"unknown data in config file: {type(file_list)=}" | ||
file_list = [ | ||
str(source_dir / file_name) | ||
for file_name in file_list | ||
] | ||
params = [ | ||
"mypy", | ||
"--strict", | ||
"--follow-imports=silent", | ||
"--no-warn-unused-ignore", | ||
"--install-types", | ||
"--non-interactive", | ||
"typings", | ||
] | ||
|
||
command = params + file_list | ||
print(" ".join(params)) | ||
|
||
try: | ||
mypy_result = subprocess.run(command) | ||
except FileNotFoundError as e: | ||
print(f"{e} - Is mypy installed?") | ||
exit(1) | ||
return mypy_result.returncode | ||
|
||
|
||
if __name__ == "__main__": | ||
# mypy is first because of its --install-types feature | ||
mypy_ret = run_mypy() | ||
pyright_ret = run_pyright() | ||
exit(mypy_ret or pyright_ret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't mypy use a separate list of files, so as the maintainer of a file they can decide which one they think should be applied for their code (assuming they are not 1:1 compatible)?
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.
We can't really isolate the checks to a specific list of files.
The list of files we give (to either mypy or pyright) is not a list of files to check, It's a list of files to report errors in. Both type checkers follow imports to get type information from other files. So the type information in any file is going to affect both checks.
If someone thinks they're only using mypy, because the file they're changing is only on the mypy list, that change will still affect the pyright check, even though the file is not on the pyright list.
Because we can't completely isolate them, I don't think it's worth it to maintain 2 separate lists.
The main focus of this is core, and worlds are opt-in.
If this PR is accepted (I don't consider it decided yet whether we want this), If a world maintainer wants to only use one, then they shouldn't opt in to this system, and should just check with their own workflow.
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.
You shouldn't think of this as a linter, checking stylistic things.
You should think of it as a static type checker, just like in a C++ or Rust compiler.
I think it wouldn't make sense to say:
"For this C++ file
x.cpp
I only care whether clang's type checker passes, but fory.cpp
I only care whether gcc's type checker passes."We would care about everything passing, in one compiler, or the other compiler, or we want to support both compilers.
I think it would be good to support multiple static type checkers, just like a C++ project that wants to support multiple compilers.
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.
That argument makes sense, yeah.
That being said, I honestly don't know how we (or who) decide(s) if we want to merge this or not.