From bfad786dcaf20fe816a895e0a2a459b953971c16 Mon Sep 17 00:00:00 2001
From: Niklas Mohrin <dev@niklasmohrin.de>
Date: Tue, 4 Jan 2022 00:25:42 +0100
Subject: [PATCH] Add `manage.py typecheck` and run it in `manage.py precommit`

---
 evap/evaluation/management/commands/precommit.py |  2 ++
 evap/evaluation/management/commands/typecheck.py | 12 ++++++++++++
 evap/evaluation/tests/test_commands.py           | 11 ++++++++++-
 3 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 evap/evaluation/management/commands/typecheck.py

diff --git a/evap/evaluation/management/commands/precommit.py b/evap/evaluation/management/commands/precommit.py
index e147efa71..455b254c8 100644
--- a/evap/evaluation/management/commands/precommit.py
+++ b/evap/evaluation/management/commands/precommit.py
@@ -16,6 +16,8 @@ class Command(BaseCommand):
             print("Please call me from the evap root directory (where manage.py resides)")
             sys.exit(1)
 
+        call_command("typecheck")
+
         # subprocess call so our sys.argv check in settings.py works
         subprocess.run(["./manage.py", "test"], check=False)  # nosec
 
diff --git a/evap/evaluation/management/commands/typecheck.py b/evap/evaluation/management/commands/typecheck.py
new file mode 100644
index 000000000..080ad6b45
--- /dev/null
+++ b/evap/evaluation/management/commands/typecheck.py
@@ -0,0 +1,12 @@
+import subprocess  # nosec
+
+from django.core.management.base import BaseCommand
+
+
+class Command(BaseCommand):
+    args = ""
+    help = "Runs the type checker (mypy)"
+    requires_migrations_checks = False
+
+    def handle(self, *args, **options):
+        subprocess.run(["mypy", "-p", "evap"], check=True)  # nosec
diff --git a/evap/evaluation/tests/test_commands.py b/evap/evaluation/tests/test_commands.py
index 128341a34..904880fda 100644
--- a/evap/evaluation/tests/test_commands.py
+++ b/evap/evaluation/tests/test_commands.py
@@ -383,6 +383,14 @@ class TestFormatCommand(TestCase):
         )
 
 
+class TestTypecheckCommand(TestCase):
+    @patch("subprocess.run")
+    def test_mypy_called(self, mock_subprocess_run):
+        management.call_command("typecheck")
+        self.assertEqual(len(mock_subprocess_run.mock_calls), 1)
+        mock_subprocess_run.assert_has_calls([call(["mypy", "-p", "evap"], check=True)])
+
+
 class TestPrecommitCommand(TestCase):
     @patch("subprocess.run")
     @patch("evap.evaluation.management.commands.precommit.call_command")
@@ -391,6 +399,7 @@ class TestPrecommitCommand(TestCase):
 
         mock_subprocess_run.assert_called_with(["./manage.py", "test"], check=False)
 
-        self.assertEqual(mock_call_command.call_count, 2)
+        self.assertEqual(mock_call_command.call_count, 3)
+        mock_call_command.assert_any_call("typecheck")
         mock_call_command.assert_any_call("lint")
         mock_call_command.assert_any_call("format")
-- 
GitLab