From: Daniele Varrazzo Date: Thu, 28 Oct 2021 15:30:40 +0000 (+0200) Subject: Make the mypy fixture available to the whole test suite X-Git-Tag: 3.0.2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d64051fab9469bd41dbbed4ec445c6c9f6b12b8d;p=thirdparty%2Fpsycopg.git Make the mypy fixture available to the whole test suite Also make mypy tests automatically slow. See #130 for other cases where a mypy fixture would be useful. --- diff --git a/tests/conftest.py b/tests/conftest.py index ca1295e76..96331e903 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 index 000000000..3ddaeb257 --- /dev/null +++ b/tests/fix_mypy.py @@ -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() diff --git a/tests/test_typing.py b/tests/test_typing.py index 71327eab0..f8c56c96f 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -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]"