diff --git a/.github/type_check.py b/.github/type_check.py index 90d41722c9a..cd6efbd9a94 100644 --- a/.github/type_check.py +++ b/.github/type_check.py @@ -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) diff --git a/.github/workflows/strict-type-check.yml b/.github/workflows/strict-type-check.yml index bafd572a26a..1c7438280ef 100644 --- a/.github/workflows/strict-type-check.yml +++ b/.github/workflows/strict-type-check.yml @@ -15,7 +15,7 @@ on: - "**.pyi" jobs: - pyright: + type-check: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -26,8 +26,8 @@ jobs: - name: "Install dependencies" run: | - python -m pip install --upgrade pip pyright==1.1.358 + python -m pip install --upgrade pip pyright==1.1.369 mypy==1.10.1 python ModuleUpdate.py --append "WebHostLib/requirements.txt" --force --yes - - name: "pyright: strict check on specific files" + - name: "type check: strict check on specific files" run: python .github/type_check.py