From 649e50253b421d08e72ba29100d7afc0efeb0559 Mon Sep 17 00:00:00 2001 From: Robert Alonso <17463757+rgalonso@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:28:52 +0000 Subject: [PATCH] test: add unittest to run mypy New unittest file consists of two tests. Both run mypy, but one does so with arguments that are expected to allow the test to pass. The other does not provide those arguments and is expected to fail. The expected failure is intentional, to serve as an ongoing reminder to try to 1) move towards having strict typing in the codebase and to 2) not add any additional errors/warnings in the meantime. Separated requirements into runtime (requirements.txt) and additional development (requirements-dev.txt). Modified devcontainer and workflow to reflect the change. --- .devcontainer/devcontainer.json | 2 +- .github/workflows/ci.yml | 2 +- requirements-dev.txt | 4 ++++ requirements.txt | 3 +-- tests/test_syntax.py | 39 +++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 requirements-dev.txt create mode 100644 tests/test_syntax.py diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ce3acb4..9ab1cda 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -12,7 +12,7 @@ // "forwardPorts": [], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install --user -r requirements.txt" + "postCreateCommand": "pip3 install --user -r requirements.txt -r requirements-dev.txt" // Configure tool-specific properties. // "customizations": {}, diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53d1400..412ba54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,6 @@ jobs: - name: "Install test dependencies" run: | python -m pip install --upgrade pip - pip install -r requirements.txt + pip install -r requirements.txt -r requirements-dev.txt - name: "Run tests" run: python -m pytest \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..8db956a --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,4 @@ +pytest==8.3.3 +mypy==1.13.0 +mypy-extensions==1.0.0 +types-requests==2.32.0.20241016 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 30ce159..af01943 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ requests==2.32.3 -ruamel.yaml==0.18.6 -pytest==8.3.3 \ No newline at end of file +ruamel.yaml==0.18.6 \ No newline at end of file diff --git a/tests/test_syntax.py b/tests/test_syntax.py new file mode 100644 index 0000000..76ae99f --- /dev/null +++ b/tests/test_syntax.py @@ -0,0 +1,39 @@ +# based on https://gist.github.com/bbarker/4ddf4a1c58ae8465f3d37b6f2234a421 + +import os +import subprocess +import sys +import unittest +from typing import List + + +class MyPyTest(unittest.TestCase): + + def __call_mypy__(self, args, files): + result: int = subprocess.call(self.base_mypy_call + args + files, env=os.environ, cwd=self.pypath) + self.assertEqual(result, 0, '') + + def test_run_mypy_app(self): + mypy_args: List[str] = [ + "--disable-error-code", "var-annotated" + ] + self.__call_mypy__(mypy_args, ["main.py"]) + + # Run test again, but without disabling any error codes. + # This is expected to fail, but we intentionally keep this test around to + # 1) try not to add any more errors to what's already in the baseline + # 2) as a reminder to try to move the codebase towards having type checking eventually + @unittest.expectedFailure + def test_run_strict_mypy_app(self): + mypy_args: List[str] = [] + self.__call_mypy__(mypy_args, ["main.py"]) + + def __init__(self, *args, **kwargs) -> None: + super(MyPyTest, self).__init__(*args, **kwargs) + my_env = os.environ.copy() + self.pypath: str = my_env.get("PYTHONPATH", os.getcwd()) + self.base_mypy_call: List[str] = [sys.executable, "-m", "mypy", "--ignore-missing-imports"] + + +if __name__ == '__main__': + unittest.main()