]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make the mypy fixture available to the whole test suite
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 28 Oct 2021 15:30:40 +0000 (17:30 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 30 Oct 2021 16:45:03 +0000 (18:45 +0200)
Also make mypy tests automatically slow.

See #130 for other cases where a mypy fixture would be useful.

tests/conftest.py
tests/fix_mypy.py [new file with mode: 0644]
tests/test_typing.py

index ca1295e76a1e4886d72f65e8d62bd4b5d9e3470f..96331e903663d6d271275f7f541402724c438510 100644 (file)
@@ -7,8 +7,9 @@ import pytest
 pytest_plugins = (
     "tests.fix_db",
     "tests.fix_pq",
-    "tests.fix_proxy",
+    "tests.fix_mypy",
     "tests.fix_faker",
+    "tests.fix_proxy",
     "tests.fix_psycopg",
 )
 
diff --git a/tests/fix_mypy.py b/tests/fix_mypy.py
new file mode 100644 (file)
index 0000000..3ddaeb2
--- /dev/null
@@ -0,0 +1,43 @@
+import re
+import subprocess as sp
+
+import pytest
+
+
+def pytest_collection_modifyitems(items):
+    # All the tests using mypy are slow
+    for item in items:
+        if "mypy" in item.fixturenames:
+            item.add_marker(pytest.mark.slow)
+
+
+@pytest.fixture(scope="session")
+def mypy(tmp_path_factory):
+    cache_dir = tmp_path_factory.mktemp(basename="mypy_cache")
+    src_dir = tmp_path_factory.mktemp("source")
+
+    class MypyRunner:
+        def run_on_file(self, filename):
+            cmdline = f"""
+                mypy
+                --strict
+                --show-error-codes --no-color-output --no-error-summary
+                --config-file= --cache-dir={cache_dir}
+                """.split()
+            cmdline.append(filename)
+            return sp.run(cmdline, stdout=sp.PIPE, stderr=sp.STDOUT)
+
+        def run_on_source(self, source):
+            fn = src_dir / "tmp.py"
+            with fn.open("w") as f:
+                f.write(source)
+
+            return self.run_on_file(str(fn))
+
+        def get_revealed(self, line):
+            """return the type from an output of reveal_type"""
+            return re.sub(
+                r".*Revealed type is (['\"])([^']+)\1.*", r"\2", line
+            ).replace("*", "")
+
+    return MypyRunner()
index 71327eab0dfe0aeb10915a6ebee75b99cf63700f..f8c56c96f83369d0c8093ab1f5ace3381c995589 100644 (file)
@@ -1,11 +1,8 @@
-import re
 import sys
-import subprocess as sp
 
 import pytest
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize(
     "filename",
     [
@@ -25,7 +22,6 @@ def test_typing_example(mypy, filename):
     assert cp.returncode == 0
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize(
     "conn, type",
     [
@@ -76,7 +72,6 @@ def test_connection_type(conn, type, mypy):
     _test_reveal(stmts, type, mypy)
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize(
     "conn, curs, type",
     [
@@ -163,7 +158,6 @@ obj = {curs}
     _test_reveal(stmts, type, mypy)
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize(
     "curs, type",
     [
@@ -195,7 +189,6 @@ obj = {await_} curs.fetchone()
     _test_reveal(stmts, type, mypy)
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize(
     "curs, type",
     [
@@ -233,7 +226,6 @@ curs = {curs}
     _test_reveal(stmts, type, mypy)
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize("method", ["fetchmany", "fetchall"])
 @pytest.mark.parametrize(
     "curs, type",
@@ -266,7 +258,6 @@ obj = {await_} curs.{method}()
     _test_reveal(stmts, type, mypy)
 
 
-@pytest.mark.slow
 @pytest.mark.parametrize("server_side", [False, True])
 @pytest.mark.parametrize("conn_class", ["Connection", "AsyncConnection"])
 def test_cur_subclass_execute(mypy, conn_class, server_side):
@@ -309,38 +300,6 @@ class MyCursor(psycopg.{cur_base_class}[Row]):
     assert types[0] == types[2]
 
 
-@pytest.fixture(scope="session")
-def mypy(tmp_path_factory):
-    cache_dir = tmp_path_factory.mktemp(basename="mypy_cache")
-    src_dir = tmp_path_factory.mktemp("source")
-
-    class MypyRunner:
-        def run_on_file(self, filename):
-            cmdline = f"""
-                mypy
-                --strict
-                --show-error-codes --no-color-output --no-error-summary
-                --config-file= --cache-dir={cache_dir}
-                """.split()
-            cmdline.append(filename)
-            return sp.run(cmdline, stdout=sp.PIPE, stderr=sp.STDOUT)
-
-        def run_on_source(self, source):
-            fn = src_dir / "tmp.py"
-            with fn.open("w") as f:
-                f.write(source)
-
-            return self.run_on_file(str(fn))
-
-        def get_revealed(self, line):
-            """return the type from an output of reveal_type"""
-            return re.sub(
-                r".*Revealed type is (['\"])([^']+)\1.*", r"\2", line
-            ).replace("*", "")
-
-    return MypyRunner()
-
-
 def _test_reveal(stmts, type, mypy):
     ignore = (
         "" if type.startswith("Optional") else "# type: ignore[assignment]"